1
0
Code Pull Requests Projects Releases Activity GitHub Gitee

add html to pdf

This commit is contained in:
kevinbzhang 2022-08-16 14:47:29 +08:00
parent dd78dd88b2
commit 64b0311595
4 changed files with 30 additions and 3 deletions

6
.gitignore vendored
View File

@ -1 +1,5 @@
node_modules
node_modules
.DS_STORE
*.pdf
*.html

27
main.js
View File

@ -1,5 +1,6 @@
const request = require('request');
const fs = require('fs');
const wkhtmltopdf = require('wkhtmltopdf');
// 请求 APi 接口
async function getApiResult(url) {
@ -64,8 +65,30 @@ async function getDetail(uuid) {
// console.log(section.sectionId);
let fileName = `${section.chapterId}.${section.sectionId} ${section.title}.html`;
let pdfFileName = `${section.chapterId}.${section.sectionId} ${section.title}.pdf`;
fs.writeFileSync(`./nowcoder/${fileName}`, section.content);
fs.writeFileSync(`./output/html/${fileName}`, section.content);
await transferToPDF(`./output/html/${fileName}`, `./output/pdf/${pdfFileName}`);
}
main();
// https://wkhtmltopdf.org/
async function transferToPDF(htmlFilePath, pdfFilePath) {
let html = `
<html>
<head>
<meta charset="utf-8">
<style>
* {
font-size: large;
}
</style>
</head>
<body>
${fs.readFileSync(htmlFilePath)}
</body>
</html>`
wkhtmltopdf(html, { pageSize: "A4" })
.pipe(fs.createWriteStream(pdfFilePath));
}
main();