我正在使用 pdfkit 和 wkhtmltopdf 从 html 生成 pdf。我使用以下代码来做到这一点:
pdfkit.from_string(htmlString, filename, options = options, cover = cover)
封面是 html 文件,选项是设置 wkhtmltopdf 选项的字典,文件名是生成的 pdf 文件的名称,htmlString 是包含 pdf 内容的字符串。
选项是:
options = {
'user-style-sheet':css,
'encoding': 'UTF-8',
'margin-left': '25mm',
'margin-right': '25mm',
'margin-bottom': '25mm',
'margin-top': '25mm'
}
我想要做的和我似乎没有开始工作的是封面上的不同边距。在封面上,我有一个背景图像,它应该从页面顶部开始(没有边距)。
我尝试为此使用 css,但没有成功。我还发现了以下建议,但我似乎做错了什么:
<head>
<meta name="pdfkit-margin-left" content="0mm" />
<meta name="pdfkit-margin-right" content="0mm" />
<meta name="pdfkit-margin-bottom" content="0mm" />
<meta name="pdfkit-margin-top" content="0mm" />
</head>
有什么建议?谢谢!
最佳答案
这是我的解决方案,使用 autoFirstPage: false
当您创建 PDFDocument 时,您可以使用 addPage
在第一页上设置边距。 :
const doc = new PDFDocument({autoFirstPage: false});
doc.addPage({margin: 36});
https://pdfkit.org/docs/guide.pdf
关于python - 使用pdfkit python在第一页上使用不同的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022073/