17

I am working on a project that uses WeasyPrint to output documents. for the life of me I cannot figure out why it is defaulting the page size to a non standard page size.

I would expect it to default to 8.5 x 11 inches. instead the pdfs come out as 8.27 x 11.69 inches.

for the life of me I cannot figure out why the page size is so off.

any ideas?

Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
  • please note, I have GROSSLY changed the content to smaller than a page, still same result – Nathan Tregillus Apr 23 '15 at 18:45
  • 9
    Why do you think that is a non-standard size? It's A4, which is standard almost everywhere in the world. – Daniel Roseman Apr 23 '15 at 18:47
  • 2
    aaaand you are correct, though I can't find where in the documentation states how you override the page size, but I finally found I should use the css @page tag. wish this was clearer in the docs – Nathan Tregillus Apr 23 '15 at 23:57

2 Answers2

28

I found that the css tag @page would allow me to change the actual page size of what the pdf would generate to:

        @page {
            size: Letter;
            margin: 0in 0.44in 0.2in 0.44in;
        }

good times trying to figure that one out.

Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
0

You can set CSS features for create pdf with weasyprint.

from weasyprint import HTML, CSS
css = CSS(string=''' @page {size: 315mm 445.5mm;} ''')
HTML(URLs[idx]).write_pdf(f'{names[i]}_{idx}.pdf', stylesheets=[css])
print('PDF is ready.')

With this way the page is saved more completely.