node.js - 角度的木偶 - 打印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();
})();
}
上面的函数是从一个角度模板调用的(通过一个按钮点击)。我遇到了以下问题,已使用列出的解决方案解决了它们 -
- 无法解析 '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
- 'global' 、 'buffer' 和 'process' 变量出现未定义的错误 - 我在 polyfill.ts 文件中为其添加了垫片
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
(window as any).process = {
version: ''
};
- 现在,当我运行所有这些之后,我仍然收到以下错误
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)
请让我知道你的想法。谢谢。
解决方案
推荐阅读
- ios - 如何检查 Swift 的子类中使用了哪个 ViewController
- c# - 如何使用 FileStream 将 zip 文件复制到远程服务器位置?
- html - 打印时设置默认比例
- angular - Angular 2 在表单中添加动态字段并将数据保存到数据库
- reactjs - 在反应 jsx 元素数组值中添加新行
- ios - 在没有 ssl 证书的 WebView 中打开 Https URL
- ios - Appium 测试甚至没有在 AWS DeviceFarm 上开始
- typescript - Typescript 'keyof' 关键字在使用 react setState 函数时出错
- ios - 从 NSObject 类创建一个数组
- visual-studio - Azure DevOps/VSTS 数据库和生产错误修复