Javascript 使用Puppeter配置PDF页面宽度
我试图生成一个pdf使用木偶,但生成的pdf是大宽度。我想有一个pdf显示在一个页面上的所有内容,必须是宽度4.8厘米,其中页面高度可以是任何长度的内容有 我在pdf中添加了配置Javascript 使用Puppeter配置PDF页面宽度,javascript,node.js,puppeteer,Javascript,Node.js,Puppeteer,我试图生成一个pdf使用木偶,但生成的pdf是大宽度。我想有一个pdf显示在一个页面上的所有内容,必须是宽度4.8厘米,其中页面高度可以是任何长度的内容有 我在pdf中添加了配置 { path : filePath, printBackground : true, width : '4.8cm' } 在页面中,我将配置添加到了viewport { width : 136, height
{
path : filePath,
printBackground : true,
width : '4.8cm'
}
在页面中,我将配置添加到了viewport
{
width : 136,
height : 842,
deviceScaleFactor : 1
}
但最终结果不会改变请尝试此代码,如果它工作正常,请选择正确答案 我在
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()
})()
您应该以像素为单位提供宽度。像素是viewport@PrabhjotSinghKainthI的默认值,表示pdf页面宽度,您使用的是4.8 cm文档,表示其有效too@PrabhjotSinghKainth我试着改变像素,但还是一样