简体   繁体   中英

Why [tag:weasyprint] xhtml-to-pdf does not fit A4 page?

I try to convert an "xhtml web page" to an "A4 portrait pdf" with weasyprint Python 3 API. Here is the page.
But the pdf file, at the end, does not fit A4 page.
Here is the python code:

#!/usr/bin/python3

from weasyprint import HTML, CSS
import subprocess

Page = HTML(url="https://educadhoc.hachette-livre.fr/extract/complet/9782401058705/show-page/page325.xhtml")
Style = CSS(string='''
    @page {
        size: A4 portrait;
        max-height:100%;
        max-width:100%;
        }
''')
Page.write_pdf(target="Try.pdf", zoom=1, stylesheets=[Style])
subprocess.Popen(["evince", "Try.pdf"])

It does not fit A4 even with "size A4 portrait" in CSS style and "zoom=1" in write_pdf method!
(There is also lines shifts in the pdf file. ...)
What do you suggest?

Yes. I changed.., Stop using Weasyprint and go to selenium, more customizable

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


######  SELENIUM PARAMETERS
path_to_binary = "/usr/bin/firefox-esr"
path_to_webdriver = "/usr/local/bin/geckodriver"
capabilities = webdriver.DesiredCapabilities().FIREFOX
my_options = Options()
my_options.headless = True
my_options.add_argument("--width=1426")
my_options.add_argument("--height=2048")
my_options.binary_location = path_to_binary
my_service = Service(path_to_webdriver)


######  GO GO GO !
driver = webdriver.Firefox(service=my_service, options=my_options)
driver.get("https://educadhoc.hachette-livre.fr/extract/complet/9782401058705/show-page/page325.xhtml")
driver.execute_script("document.body.style.transform = 'scale(3)'")
driver.find_element_by_tag_name("body").screenshot("TRY.png")
driver.implicitly_wait(2)
driver.close()
  • It works like a charm but in a png image file (not too bad); the original web page is 1426x2048 according to the Firefox inspector.
  • I put a scale(3) that you can adjust according to you in the driver.execute_script line in order to have a 3x more high image size and quality... (but 3x more heavy !)
  • You need to install a webdriver (the firefox's one in my case) in the correct path ( /usr/local/bin/geckodriver in my case) and you have to locate the binary path too ( /usr/local/bin/firefox-esr in my case).
  • The last webdriver of Firefox (for Linux, Mac or Win) is downloadable at: https://github.com/mozilla/geckodriver/releases
  • Then, you can do what you want with this PNG file. (eg use imagemagick to convert into differnet format like pdf if you want...) Link to Convert

  • Hope it helps you and the others Python beginners...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM