Kaynağa Gözat

简历制作

洪海涛 3 yıl önce
ebeveyn
işleme
fbbcf7e923
4 değiştirilmiş dosya ile 55 ekleme ve 17 silme
  1. 2 0
      .gitignore
  2. 8 0
      README.md
  3. 14 7
      bin/html2pdf.mjs
  4. 31 10
      bin/index.mjs

+ 2 - 0
.gitignore

@@ -1,4 +1,6 @@
 node_modules
 .idea
 *.md
+!README.md
 *.pdf
+*.html

+ 8 - 0
README.md

@@ -0,0 +1,8 @@
+### 安装依赖
+```shell
+pnpm install
+```
+
+### 使用
+
+

+ 14 - 7
bin/html2pdf.mjs

@@ -1,26 +1,33 @@
 import puppeteer from "puppeteer";
 import fs from 'node:fs'
 import path from 'node:path'
-import {fileURLToPath} from 'node:url';
+import { fileURLToPath } from 'node:url';
 
-async function run() {
+async function run(fileName) {
+  const pdfName = `${fileName}`.replace('.html', '.pdf')
   try {
     const browser = await puppeteer.launch({
       // executablePath: '/Applications/Google\ Chrome.app/Contents/MacOS/bin',
       // ignoreHTTPSErrors: false, // 在导航期间忽略 HTTPS 错误
       // headless: false,
       defaultViewport: { // 为每个页面设置一个默认视口大小
-        width: 1920,
+        width: 1057,
         height: 1080
       }
     });
     const page = await browser.newPage();
-    await page.goto('file:///Users/honghaitao/code/node_project/md2pdf/index.html');
+    await page.goto(`file://${process.env.PWD}/${fileName}`);
     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})
+    console.log(offsetHeight,21);
+    console.log(inputElement)
+    try {
+      fs.unlinkSync(pdfName);
+    }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: 'index_002.pdf', format:'A4'})
     await browser.close();
   } catch ( e ) {
     console.log(e)

+ 31 - 10
bin/index.mjs

@@ -21,20 +21,41 @@ marked.setOptions({
   xhtml: false
 });
 
-const key = process.argv[2]
-const filePath = process.argv[3]
-// console.log(22, filePath)
+const filePath = `${process.argv[3]}`
 const markdownString = fs.readFileSync(filePath, 'utf8')
+const fileName = `${filePath}`.replace('.md','.html')
+console.log(fileName)
 let html = marked.parse(markdownString)
-// <link rel="stylesheet"
-//       href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css">
-console.log(html)
-path.
 html = `<link rel="stylesheet"
-      href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github.min.css">${html}`
+      href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github.min.css">
+<style>
+*{
+    font-family: "PingFang TC";
+}
+h2 {
+margin-bottom: 5px;
+}
+h3 {
+margin-bottom: 0;
+}
+p{
+margin: 5px 0;
+}
+ol{
+margin: 5px 0;
+}
+</style>
+${html}`
+try {
+  fs.unlinkSync(fileName);
+} catch ( e ) {
+  console.log(e)
+}
+fs.writeFile(fileName, html, { flag: 'a+' }, (err) => {
+  if (err) throw err;
+  run(fileName)
+})
 
-fs.writeFileSync('index.html', html, { flag: 'a+' })
-run()
 // console.log(marked.parse(markdownString));
 // import github from 'highlight.js/styles/github.css'
 // const github = fs.readFileSync('highlight.js/styles/github.css', 'utf8')