NodeJs-Puppeteer:为什么下面的页面大小设置无效?

问题描述 投票:0回答:1

我试图设置PDF的页面大小,使其成为横向,但失败。我应该进行哪些更改才能使其生效?

我试图添加page.setViewportisLandscape很多时间,但仍然没有使它生效。

browser = await puppeteer.launch({
    executablePath: '/usr/bin/chromium-browser',
    args: ['--no-sandbox', '--enable-font-antialiasing', '--font-render-hinting=medium'], //, '--window-size=1070x1514', '--disable-dev-shm-usage', '--disable-gpu', '--disable-software-rasterizer'
    timeout: LOAD_TIMEOUT,
    headless: true
    ,isLandscape: true
});

page = await browser.newPage();

await page.setViewport({
    width: 1080,
    height: 1600,
    deviceScaleFactor: 1,
    isLandscape: true
});

// local file
await page.goto(`file:///${ __dirname}/www/index.html`, {
    waitUntil: 'domcontentloaded',
    timeout: LOAD_TIMEOUT
});

await page.waitForFunction(() => !!(window.Ext && Ext.isReady && window.App && App.app), {
    polling: LOAD_POLLING,
    timeout: LOAD_TIMEOUT
});
await page.setViewport({
    width: 1080,
    height: 1600,
    deviceScaleFactor: 1,
    isLandscape: true
});

await page.evaluate(
    App.pdf.Builder.create({
        ...
    });
);
await page.waitForFunction(() => App.pdf.Builder.ready || App.pdf.Builder.error, {
    polling: LOAD_POLLING,
    timeout: PAGEBUILD_TIMEOUT
});

await page.pdf({
    path: filePath,
    format: 'A4',
    margin: {
        top: '0px',
        right: '0px',
        bottom: '0px',
        left: '0px'
    },
    printBackground: true // required for photos otherwise blank
    ,scale: 0.5
});

我试图在width: '1920px', height: '1080px'中添加page.pdf(),但也失败了。 (只有在page.pdf()中设置,才能使其变为横向)

node.js size puppeteer
1个回答
1
投票

根据to the docs,您还需要告诉page.pdf方法您要使用景观PDF:

await page.pdf({
    landscape: true, // <-- must be set to true to get a landscape PDF
    path: filePath,
    format: 'A4',
    margin: {
        top: '0px',
        right: '0px',
        bottom: '0px',
        left: '0px'
    },
    printBackground: true // required for photos otherwise blank
    ,scale: 0.5
});
© www.soinside.com 2019 - 2024. All rights reserved.