我在我的Rails应用程序中使用PDFKit来生成PDF.问题是我的一些内容包含非ascii字符.如何强制它使用UTF-8?
我正在尝试使用PDFKit.
它看起来相当简单,所以我试图跟随Ryan的Rails演员.
所以我将gem添加到我的GemFile并更新了我application.rb:
config.middleware.use "PDFKit::Middleware"
Run Code Online (Sandbox Code Playgroud)
之后我在linux上安装了wkhtmltopdf:
apt-get install wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)
我确保它有效:
[nicolas@Minto]%wkhtmltopdf www.google.com gogole.pdf
Loading page (1/2)
Printing pages (2/2)
Done
Run Code Online (Sandbox Code Playgroud)
在我尝试在我的一个页面上添加.pdf之前,一切看起来都很好......
该网站永远处于加载状态.当我Ctrl - C在控制台中使用a停止服务器时,出现以下错误:
RuntimeError (command failed: "/usr/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"):
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:71:in `to_pdf'
pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in `call'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我需要在node.js中使用pdfkit生成pdf文件:
我有这个代码:
doc = new PDFDocument;
doc.pipe; fs.createWriteStream('output.pdf');
doc.fontSize(15);
doc.text('Generate PDF!');
doc.end();
Run Code Online (Sandbox Code Playgroud)
它将创建一个pdf文件:'output.pdf',但每次打开文件时,文件都会损坏.它说:'无法打开文档不支持文件类型纯文本文档(text/plain)'.
然后我尝试了:
doc = new PDFDocument;
doc.pipe fs.createWriteStream('output.pdf');
doc.fontSize(15);
doc.text('Generate PDF!');
doc.end();
Run Code Online (Sandbox Code Playgroud)
但是有一个错误说:
doc.pipe fs.createWriteStream('output.pdf');
^^
Syntax error: Unexpected Identifier
Run Code Online (Sandbox Code Playgroud)
那么,在nodejs中生成pdf文件的正确方法是什么?特别是部分:
doc.pipe fs.createWriteStream
Run Code Online (Sandbox Code Playgroud)
请帮我.提前致谢!
是否可以使用PDFkit将文本中的"ěščřžýáé"等字符保存为PDF?
他们的浏览器演示在这里:http: //pdfkit.org/demo/browser.html
注意:我只使用js库,没有node.js等.
我在下面重现了我的问题:
我在网页上画了一个 210x297 的矩形
<!DOCTYPE html>
<html>
<style>
div.rectangle {
border: solid 1px;
width: 210mm;
height: 297mm;
}
</style>
<head>
</head>
<body>
<div class="rectangle">
<img/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)我使用 Python 中的 pdfkit 将此 html 文档转换为 pdf
import pdfkit
options = {
'page-size':'A4',
'encoding':'utf-8',
'margin-top':'0cm',
'margin-bottom':'0cm',
'margin-left':'0cm',
'margin-right':'0cm'
}
pdfkit.from_file('test.html', 'test.pdf', options=options)
Run Code Online (Sandbox Code Playgroud)我获得了一个 pdf 文件,左上角有一个矩形,其大小大约小了 5 倍......
如果您能看看,我将不胜感激!
我使用 node.js 请求从 URL 获取 JPEG。然后我将图像转换为 base64 缓冲区并将其写入 pdf 文档:
request({ url: url, encoding: null }, function (error, response, body) {
if (!error && response.statusCode == 200) {
var img = new Buffer(body, 'base64');
pdf.image(img, 0, 0);
callback(error, body);
}
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
events.js:160
throw er; // Unhandled 'error' event
^
Error: stream.push() after EOF
at readableAddChunk (_stream_readable.js:156:17)
at PDFDocument.Readable.push (_stream_readable.js:134:10)
at PDFDocument._write (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/document.js:156:12)
at PDFReference.finalize (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/reference.js:81:21)
at PDFReference.finalize (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/reference.js:10:59)
at PDFReference.end (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/reference.js:74:21)
at JPEG.embed (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/image/jpeg.js:68:16)
at PDFDocument.image (/Users/assafshamia/Freebird/node/utils/report_generator/node_modules/pdfkit/js/mixins/images.js:34:15)
at addImage …Run Code Online (Sandbox Code Playgroud) 使用iOS11 Swift和PDFKit.我有一份约300页的pdf文件.每页可能有100个字符.整个文件大小约为4mb.当我将文件加载到PDFView时,应用程序内存增加到30mb +.当我滑到下一页时,它再增加30mb +.如果I.滑动到pdf的末尾,我在Xcode中遇到了内存崩溃.那么当该页面不再可见时,如何从内存中清除PDFPage?
码
func loadPDFDocument(){
let documentURL = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!
if let document = PDFDocument(url: documentURL) {
for page in 1...document.pageCount {
DispatchQueue.global().async {
print(document.page(at: page)!.string!)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个 PDF 套件,我想将文件保存为 A4 大小
这是我的代码
let newPagetxt = PDFPage(image:image!)
let apdf = PDFDocument()
apdf.insert(newPagetxt!, at:index)
apdf.documentAttributes!["Title"] = sampleFilenameTitle
apdf.documentAttributes!["Author"] = sampleFilenameAuthre
apdf.write(to: destination)
Run Code Online (Sandbox Code Playgroud)
如何使用一个标准类在 A4 中设置尺寸
如何在PDFView使用PDFKit 时禁用滚动反弹?
显示 PDF 的视图没有滚动弹跳选项。
这是我的代码:
if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoresizesSubviews = true
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight,
.flexibleTopMargin, .flexibleLeftMargin]
pdfView.autoScales = true
pdfView.displaysPageBreaks = true
pdfView.displayDirection = .vertical
pdfView.displayMode = .singlePageContinuous
pdfView.document = pdfDocument
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢(可能是一个非常简单的问题!)
我正在尝试将 html 模板代码中的 pdf 放在一起。我遇到的问题是我尝试过的 html-pdf 转换器没有正确格式化 css,我猜它们还与 css flex-box 不兼容。
我研究了两种方法:
在 Node.js 上使用 html-pdf 包
在python中使用jinja2、pdfkit和wkhtmltopdf
这两种方法都允许我对 html 进行模板化并从 html 动态构建 pdf,但 css 转换不能 100% 工作(css flex-box 失败)。
关于可以处理css flex-box的html到pdf转换器的任何想法?或者,有没有人知道上述方法的任何修复程序,以便他们可以处理 css flex-box?只想从 css flex-box 样式的 html 制作 pdf。
谢谢!
pdfkit ×10
ios ×2
javascript ×2
node.js ×2
swift ×2
swift4 ×2
utf-8 ×2
wkhtmltopdf ×2
css ×1
html ×1
html-pdf ×1
html-to-pdf ×1
installation ×1
ios11 ×1
node-pdfkit ×1
pdf ×1
python ×1
scroll ×1
uiscrollview ×1