123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import puppeteer from "puppeteer";
- import fs from 'node:fs'
- import path from 'node:path'
- import {fileURLToPath} from 'node:url';
- async function run() {
- try {
- const browser = await puppeteer.launch({
- // executablePath: '/Applications/Google\ Chrome.app/Contents/MacOS/bin',
- // ignoreHTTPSErrors: false, // 在导航期间忽略 HTTPS 错误
- // headless: false,
- defaultViewport: { // 为每个页面设置一个默认视口大小
- width: 1920,
- height: 1080
- }
- });
- const page = await browser.newPage();
- await page.goto('file:///Users/honghaitao/code/node_project/md2pdf/index.html');
- let inputElement = await page.$('html');
- const offsetHeight = await page.evaluate(() => document.querySelector('body').offsetHeight);
- // console.log(offsetHeight);
- // console.log(inputElement)
- await page.pdf({path: 'index_002.pdf',height: offsetHeight,width: 1920})
- await browser.close();
- } catch ( e ) {
- console.log(e)
- }
- }
- export default run;
- // const filePath = path.join(__dirname, "index.html")
- // console.log(35, filePath)
- // const __filename = fileURLToPath(import.meta.url);
- //
- // // 👇️ "/home/john/Desktop/javascript"
- // const __dirname = process.cwd()
- // console.log('directory-name 👉️', __dirname);
- //
- // console.log('2222', process.execPath)
- // console.log('2222', process.cwd())
|