Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent webpdf to split in pages #1402

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions nbconvert/exporters/webpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WebPDFExporter(HTMLExporter):
export_from_notebook = "PDF via pyppeteer"

allow_chromium_download = Bool(False,
help="Whether to allow downloading Chromium if no suitable version is found on the system."
help='Whether to allow downloading Chromium if no suitable version is found on the system.'
).tag(config=True)

def _check_launch_reqs(self):
Expand All @@ -39,7 +39,32 @@ async def main():
browser = await self._check_launch_reqs()()
page = await browser.newPage()
await page.goto('data:text/html,'+html, waitUntil='networkidle2')
pdf_data = await page.pdf()

dimensions = await page.evaluate(
"""() => {
return {
width: document.body.scrollWidth,
height: document.body.scrollHeight,
}
}"""
)
width = dimensions['width']
height = dimensions['height']

pdf_data = await page.pdf(
{
'width': width,
# 200 inches is the maximum height for Adobe Acrobat Reader.
'height': min(height, 200 * 72),
'printBackground': True,
'margin': {
'left': '0px',
'right': '0px',
'top': '0px',
'bottom': '0px',
},
}
)
await browser.close()
return pdf_data

Expand All @@ -52,7 +77,7 @@ def from_notebook_node(self, nb, resources=None, **kw):
nb, resources=resources, **kw
)

self.log.info("Building PDF")
self.log.info('Building PDF')
pdf_data = self.run_pyppeteer(html)
self.log.info('PDF successfully created')

Expand Down