add html to pdf
This commit is contained in:
parent
dd78dd88b2
commit
64b0311595
6
.gitignore
vendored
6
.gitignore
vendored
@ -1 +1,5 @@
|
||||
node_modules
|
||||
node_modules
|
||||
.DS_STORE
|
||||
|
||||
*.pdf
|
||||
*.html
|
27
main.js
27
main.js
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user