首页 > 解决方案 > 角度的木偶 - 打印pdf

问题描述

我正在尝试在 Angular 9 应用程序中打印 pdf(将 html 页面转换为 pdf)。我使用 puppeteer 来完成同样的任务 - 但遇到了问题 -

下面是我的代码

exportPdf(){
    const url = window.location.href;
    (async () => {
      const browser = await puppeteer.launch({headless:true});
      const page = await browser.newPage();
      await page.goto(url);
      await page.pdf({path:'medium.pdf'});
      await browser.close();
    })();

  }

上面的函数是从一个角度模板调用的(通过一个按钮点击)。我遇到了以下问题,已使用列出的解决方案解决了它们 -

  1. 无法解析 'ws' - 我通过 npm install 安装了 ws 并修改了 WebSocketTransport.js 以读取 - node_modules/ws
const WebSocket = require('../node_modules/ws'); --> after adding the path error disappeared
const WebSocket = require('ws') --> this was not working
  1. 'global' 、 'buffer' 和 'process' 变量出现未定义的错误 - 我在 polyfill.ts 文件中为其添加了垫片
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
(window as any).process = {
    version: ''
  };
  1. 现在,当我运行所有这些之后,我仍然收到以下错误
Uncaught TypeError: Cannot read property 'PUPPETEER_PRODUCT' of undefined
    at Launcher (Launcher.js:855)
    at Object.get _launcher [as _launcher] (Puppeteer.js:63)
    at Object.get (<anonymous>)
    at Function.installAsyncStackHooks (helper.js:106)
    at Object../node_modules/puppeteer/index.js (index.js:22)
    at __webpack_require__ (bootstrap:84)
    at Module../src/app/shared/export-pdf/export-pdf.component.ts (tooltip.directive.ts:29)
    at __webpack_require__ (bootstrap:84)
    at Module../src/app/shared/shared.module.ts (shared.module.ts:1)
    at __webpack_require__ (bootstrap:84)

请让我知道你的想法。谢谢。

标签: node.jsangularpdf-generationnode-modulespuppeteer

解决方案


推荐阅读