我正在尝试使用puppeteer生成pdf,但生成的pdf宽度很大。我想有pdf格式,显示在一个页面上的所有内容,必须是4.8厘米的宽度,其中页面高度可以是其内容的任何长度。
我将配置添加到pdf
{
path : filePath,
printBackground : true,
width : '4.8cm'
}在页面上,我将配置添加到了视口
{
width : 136,
height : 842,
deviceScaleFactor : 1
}但是最终的结果并没有改变。
发布于 2019-12-24 19:46:06
尝试此代码,并选择此答案作为正确答案,如果它工作得很好。
我在page.pdf方法中设置了缩放选项,以将原始打印的PDF缩放为缩放版本。因此,这使得整个页面只适合一页。
const puppeteer = require('puppeteer')
const pageURL = 'https://stackoverflow.com/questions/59464250/configure-pdf-page-width-using-puppeteer'
const optionsPDF = {
path: 'print.pdf',
printBackground: true,
width: '4.8cm',
scale: 1,
preferCSSPageSize: false
}
;(async () => {
const browser = await puppeteer.launch({
headless: true,
devtools: false,
defaultViewport: {
width : 136,
height : 842,
deviceScaleFactor : 1
}
})
const [page] = await browser.pages()
await page.emulateMedia('screen')
await page.goto( pageURL, { waitUntil: 'networkidle0', timeout: 0 })
const height_weight_ratio = await page.evaluate( () => window.innerHeight / window.innerWidth)
const height = parseInt(optionsPDF.width) * height_weight_ratio
optionsPDF.scale = 1/height_weight_ratio
console.log('Printing PDF...')
await page.pdf(optionsPDF)
await browser.close()
})()https://stackoverflow.com/questions/59464250
复制相似问题