Reputation: 41
I am generating a pdf from html using pdfkit and wkhtmltopdf. I use the following code to do this:
pdfkit.from_string(htmlString, filename, options = options, cover = cover)
with cover being a html file, options a dict which set wkhtmltopdf options, filename the name of the resulting pdf file and htmlString is the string containing the content for the pdf.
Options are:
options = {
'user-style-sheet':css,
'encoding': 'UTF-8',
'margin-left': '25mm',
'margin-right': '25mm',
'margin-bottom': '25mm',
'margin-top': '25mm'
}
What I want to do and what I don't seem to get to work is different margins on the cover page. On the cover I have a background image that should start at the top of the page (without margin).
I tried to use css for this but without succes. I also found the following suggestion but I seem to be doing something wrong:
<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>
Any suggestions? Thank you!
Upvotes: 4
Views: 5590
Reputation: 31
Here is my solution, use autoFirstPage: false
when you create the PDFDocument, then you can set the margin on the first page using addPage
:
const doc = new PDFDocument({autoFirstPage: false});
doc.addPage({margin: 36});
https://pdfkit.org/docs/guide.pdf
Upvotes: 3