jsPDF 是一个基于 HTML5 的客户端解决方案,用于生成各种用途的 PDF 文档,jsPDF 是一个使用 JavaScript 语言生成 PDF 的开源库。
网址:https://parall.ax/products/jspdf
尝试一下>>
window.jsPDF = window.jspdf.jsPDF;
function test(){
var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is PDF.');
doc.addPage();
doc.text(20, 20, 'this is page 2?');
doc.addPage();
doc.text(20, 20, 'this is page 3?');
doc.save('Test.pdf');
}
生成pdf的内容不能为中文,会乱乱码
//没有效果
//添加字体文本
doc.addFileToVFS('blobs',addFontBase64());
//添加字体,字体名,格式
doc.addFont('blobs','font_bolds','normal')
//通过字体名,使用这字体
doc.setFont('font_bolds')
html2pdf.js是基于JavaScript的客户端PDF生成库,可将HTML元素转换为PDF文件。
尝试一下>>
wkhtmltopdf.exe是一个基于WebKit引擎的开源命令行工具,可将HTML页面转换为高质量PDF文档,支持页眉页脚、水印、目录等功能,完全免费且跨平台。
网址:https://wkhtmltopdf.org/downloads.html
<%@ Language=VBScript %>
<%
response.addheader "Content-Type", "text/html; charset=utf-8"
'使用wkhtmltopdf从ASP生成PDF的示例
'首先需要下载并安装wkhtmltopdf: https://wkhtmltopdf.org/downloads.html
'创建临时HTML文件
Dim fso, htmlFile, htmlPath, pdfPath, htmlContent
Set fso = Server.CreateObject("Scripting.FileSystemObject")
'设置临时文件路径(请确保IIS有写入权限)
Dim tempFolder
tempFolder = Server.MapPath("./")
'如果临时文件夹不存在则创建
If Not fso.FolderExists(tempFolder) Then
fso.CreateFolder(tempFolder)
End If
'生成唯一的文件名
Dim uniqueID
uniqueID = Int(Rnd() * 1000)
htmlPath = tempFolder & "/temp_" & uniqueID & ".html"
pdfPath = tempFolder & "/output_" & uniqueID & ".pdf"
'创建HTML内容
htmlContent = "<html><head>" & _
"<meta charset='utf-8'>" & _
"<title>PDF报表</title>" & _
"<style>" & _
"body { font-family: SimSun, Arial, sans-serif; }" & _
"h1 { color: #003366; text-align: center; }" & _
"table { width: 100%; border-collapse: collapse; }" & _
"table, th, td { border: 1px solid #ccc; }" & _
"th { background-color: #f0f0f0; padding: 8px; }" & _
"td { padding: 8px; }" & _
".footer { text-align: center; margin-top: 30px; font-size: 12px; }" & _
"</style></head>" & _
"<body>" & _
"<h1>PDF导出示例文档</h1>" & _
"<p>这是通过wkhtmltopdf生成的PDF文档</p>" & _
"<p>生成时间: " & Now() & "</p>" & _
"<h2>示例表格</h2>" & _
"<table>" & _
"<tr><th>ID</th><th>名称</th><th>日期</th></tr>" & _
"<tr><td>1</td><td> 11 </td><td>2023-01-01</td></tr>" & _
"<tr><td>2</td><td> 222 </td><td>2023-01-02</td></tr>" & _
"<tr><td>3</td><td> 333 </td><td>2023-01-03</td></tr>" & _
"</table>" & _
"<div class='footer'>页脚信息 - 公司名称111</div>" & _
"</body></html>"
' response.write("htmlPath=" & htmlPath):response.end()
'写入HTML文件
Set htmlFile = fso.CreateTextFile(htmlPath, True)
htmlFile.Write htmlContent
htmlFile.Close
'调用wkhtmltopdf转换HTML到PDF
Dim wsh, cmd
Set wsh = Server.CreateObject("WScript.Shell")
'wkhtmltopdf命令路径(请根据实际安装位置修改)
Dim wkhtmltopdfPath
wkhtmltopdfPath = "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
'构建命令(添加更多参数可以控制页边距、页眉页脚等)
cmd = """" & wkhtmltopdfPath & """ --encoding utf-8 --page-size A4 """ & htmlPath & """ """ & pdfPath & """"
'执行命令
wsh.Run cmd, 0, True
'检查PDF文件是否成功生成
If fso.FileExists(pdfPath) Then
'设置响应头,让浏览器下载PDF
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Disposition", "attachment; filename=导出报表.pdf"
'读取PDF文件并输出到浏览器
Dim pdfFile, pdfContent
Set pdfFile = fso.GetFile(pdfPath)
Set pdfContent = pdfFile.OpenAsTextStream(1, -1) '以二进制方式打开
Response.BinaryWrite pdfContent.ReadAll()
pdfContent.Close
'清理临时文件
If fso.FileExists(htmlPath) Then fso.DeleteFile(htmlPath)
If fso.FileExists(pdfPath) Then fso.DeleteFile(pdfPath)
Else
Response.ContentType = "text/html"
Response.Write "<h2>PDF生成失败</h2>"
Response.Write " 请检查wkhtmltopdf是否正确安装,以及路径是否正确。 "
End If
'释放对象
Set htmlFile = Nothing
Set pdfContent = Nothing
Set pdfFile = Nothing
Set wsh = Nothing
Set fso = Nothing
%>