Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different margins on first page using pdfkit python

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!

like image 975
Kenny Goossens Avatar asked Oct 20 '25 02:10

Kenny Goossens


1 Answers

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

like image 123
user10094212 Avatar answered Oct 21 '25 16:10

user10094212



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!