简体   繁体   English

使用无头的chrome制作PDF(木偶)

[英]Usage of chrome headless for making PDF (puppeteer)

I wondering how can I get PDF using Chrome Headless (for example puppeteer). 我想知道如何使用Chrome Headless(例如puppeteer)获取PDF。 It seems like a good PDF maker but only on chrome using @media print. 看起来像是一个不错的PDF制造商,但仅在使用@media打印的chrome上。 So here is my question: 所以这是我的问题:

Can I get PDF by puppeteer on another browser (ie, mozilla) too? 我也可以在其他浏览器(例如mozilla)上通过puppeteer获取PDF吗? I think I can do that if I want print static page with no inputs. 我想如果我想不输入任何内容打印静态页面就可以这样做。 But if I have inputs for users and they are saving it on IE. 但是,如果我为用户提供输入并且他们将其保存在IE上。 Can I use this somehow? 我可以以某种方式使用它吗?


Ok i downloaded the puppeteer. 好的,我下载了木偶。 I've got the code: 我有代码:

$scope.aClick = function(){
        const puppeteer = require('puppeteer');

        (async () => {
          const browser = await puppeteer.launch();
          const page = await browser.newPage();
          await page.goto('/vUrl_form.html', {waitUntil: 'networkidle'});
          await page.pdf({path: 'images/asd.pdf', format: 'A4'});

          browser.close();
        })();
    };

and this can't still work (i don't know why, but app can't run). 而且这仍然无法正常工作(我不知道为什么,但是应用无法运行)。

否-Puppeteer仅适用于Chromium / Chrome。

Unfortunately Puppeteer only works with Chromium/Chrome. 不幸的是,Puppeteer仅适用于Chromium / Chrome。

If you want to use Headless Mozilla Firefox, you might consider checking this out https://developer.mozilla.org/en-US/Firefox/Headless_mode . 如果要使用Headless Mozilla Firefox,则可以考虑查看https://developer.mozilla.org/en-US/Firefox/Headless_mode

If you still want to use Puppeteer, here is a working snippet that creates a .pdf file: 如果您仍然想使用Puppeteer,请使用以下可创建.pdf文件的代码段:

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle'});
// page.pdf() is currently supported only in headless mode.
// @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
await page.pdf({
  path: 'hn.pdf',
  format: 'letter'
});

browser.close();

})();

Today it's possible to use firefox with puppeter https://firefox-puppeteer.readthedocs.io/en/master/ Maybe when people answered it wasn't. 今天,可以将火狐与伪造者一起使用https://firefox-puppeteer.readthedocs.io/en/master/也许当人们回答不是这样时。 But I can't find url to pdf functionality. 但是我找不到pdf功能的网址。 Just screenshots. 只是截图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM