john há 2 anos atrás
pai
commit
3c39f311ad
3 ficheiros alterados com 19 adições e 10 exclusões
  1. 1 1
      README.md
  2. 5 3
      bin/html2pdf.mjs
  3. 13 6
      bin/index.mjs

+ 1 - 1
README.md

@@ -5,4 +5,4 @@ pnpm install
 
 ### 使用
 
-
+md2pdf 二维码生成文档-精简版本2.md .

+ 5 - 3
bin/html2pdf.mjs

@@ -4,6 +4,7 @@ import path from 'node:path'
 import { fileURLToPath } from 'node:url';
 
 async function run(fileName) {
+  console.log(7, fileName)
   const pdfName = `${fileName}`.replace('.html', '.pdf')
   try {
     const browser = await puppeteer.launch({
@@ -11,12 +12,13 @@ async function run(fileName) {
       // ignoreHTTPSErrors: false, // 在导航期间忽略 HTTPS 错误
       // headless: false,
       defaultViewport: { // 为每个页面设置一个默认视口大小
-        width: 1057,
+        width: 1240,
         height: 1080
       }
     });
     const page = await browser.newPage();
-    await page.goto(`file://${process.env.PWD}/${fileName}`);
+    // await page.goto(`file://${process.env.PWD}/${fileName}`);
+    await page.goto(`file://${fileName}`);
     let inputElement = await page.$('html');
     const offsetHeight = await page.evaluate(() => document.querySelector('body').offsetHeight);
     console.log(offsetHeight,21);
@@ -26,7 +28,7 @@ async function run(fileName) {
     }catch ( e ) {
       console.log(e)
     }
-    await page.pdf({path: pdfName, height: offsetHeight + 110,width: 1057,margin: {left: 20,top: 20,right: 20,bottom: 20}})
+    await page.pdf({path: pdfName, height: offsetHeight + 110,width: 1240,margin: {left: 20,top: 20,right: 20,bottom: 20}})
     // await page.pdf({path: 'index_002.pdf', format:'A4'})
     await browser.close();
   } catch ( e ) {

+ 13 - 6
bin/index.mjs

@@ -20,14 +20,16 @@ marked.setOptions({
   smartypants: false,
   xhtml: false
 });
-
-const filePath = `${process.argv[3]}`
+const filePath = `${process.env.PWD}/${process.argv[2]}`
 const markdownString = fs.readFileSync(filePath, 'utf8')
 const fileName = `${filePath}`.replace('.md','.html')
-console.log(fileName)
 let html = marked.parse(markdownString)
-html = `<link rel="stylesheet"
-      href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github.min.css">
+html = html.replace(/!\[\[(.*)\]\]/g, '<img src="$1" />')
+html = `
+<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github.min.css">
+      <link rel="stylesheet"
+      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
+
 <style>
 *{
     font-family: "PingFang TC";
@@ -45,7 +47,12 @@ ol{
 margin: 5px 0;
 }
 </style>
-${html}`
+<body>
+${ html }
+</body>
+<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
+<script>hljs.highlightAll();</script>
+`
 try {
   fs.unlinkSync(fileName);
 } catch ( e ) {