Skip to content

Commit

Permalink
Make pagination configurable in webpdf (#1513)
Browse files Browse the repository at this point in the history
* Enable pagination by default in webpdf

IMO, webpdf should be a 'drop-in' replacement for
the LaTeX PDF generation as much as possible. The default
should be to paginate, and it can be turned off by
config if needed.

Ref #1464

* Default pagination to True for real
  • Loading branch information
yuvipanda authored Feb 3, 2021
1 parent 71b592a commit e78bbfe
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions nbconvert/exporters/webpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class WebPDFExporter(HTMLExporter):
help='Whether to allow downloading Chromium if no suitable version is found on the system.'
).tag(config=True)

paginate = Bool(
True,
help="""
Split generated notebook into multiple pages.
If False, a PDF with one long page will be generated.
Set to True to match behavior of LaTeX based PDF generator
"""
).tag(config=True)

def _check_launch_reqs(self):
try:
from pyppeteer import launch
Expand All @@ -49,27 +60,29 @@ async def main():
await page.goto('data:text/html,'+html, waitUntil='networkidle0')
await page.waitFor(100)

# Floating point precision errors cause the printed
# PDF from spilling over a new page by a pixel fraction.
dimensions = await page.evaluate(
"""() => {
const rect = document.body.getBoundingClientRect();
return {
width: Math.ceil(rect.width) + 1,
height: Math.ceil(rect.height) + 1,
pdf_params = {}
if not self.paginate:
# Floating point precision errors cause the printed
# PDF from spilling over a new page by a pixel fraction.
dimensions = await page.evaluate(
"""() => {
const rect = document.body.getBoundingClientRect();
return {
width: Math.ceil(rect.width) + 1,
height: Math.ceil(rect.height) + 1,
}
}"""
)
width = dimensions['width']
height = dimensions['height']
# 200 inches is the maximum size for Adobe Acrobat Reader.
pdf_params = {
'width': min(width, 200 * 72),
'height': min(height, 200 * 72),
'printBackground': True,
}
}"""
)
width = dimensions['width']
height = dimensions['height']
# 200 inches is the maximum size for Adobe Acrobat Reader.
pdf_data = await page.pdf(
{
'width': min(width, 200 * 72),
'height': min(height, 200 * 72),
'printBackground': True,
}
)
pdf_data = await page.pdf(pdf_params)

await browser.close()
return pdf_data

Expand Down

0 comments on commit e78bbfe

Please sign in to comment.