I am using python playwright and I am currently opening a html file using the chromium browser. My goal is to export the exported the “rendered” file to a pdf with a single page containing the pages content using the Playwright Page PDF Api.
Currently I do the following things:
- Opening Chromium Instance using playwright
- Open page at html url
- Getting the
BoundingClientRect
of the body - Passing height and width to
pdf
function of the playwrightPage
The problem:
The little bit of content spills over to the next page. See:
What I have tried:
I tried fiddling with the height and width. When adding a constant height more content is on the first page (but sometimes it still spills over). When using a big width the problem vanishes (but the pdf looks ugly). I think the line wrapping might be a problem because when not present there is no spilling.
Here is a code for a minimal reproduction of my problem:
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.wait_for_timeout(100)
await page.goto(
'file:///path/to/file.html',
wait_until='networkidle',
)
await page.wait_for_timeout(100)
dimensions = await page.evaluate(
"""
() => {
const rect = document.body.getBoundingClientRect();
return {
"width": rect.width,
"height": rect.height
}
}
"""
)
await page.pdf(width=str(dimensions['width']), height=str(dimensions['height']), path='test.pdf')
if __name__ == '__main__':
asyncio.run(main())
Here is the link to a sample HTML I am using.