3
votes

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!

1
I am trying to figure out how to set the first page margins too. Did you figure anything out on this?GavinR

1 Answers

2
votes

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