1
0
Code Pull Requests Projects Releases Activity GitHub Gitee

添加html直接转pdf函数;图片限制最大宽度;文字添加最小限制,避免文字太小看不清

This commit is contained in:
程序员小墨 2022-08-17 00:32:51 +08:00
parent 64b0311595
commit d069b3c551

37
main.js
View File

@ -48,6 +48,7 @@ async function main() {
console.log(res);
}
}
console.log("完成");
}
async function getDetail(uuid) {
@ -73,13 +74,21 @@ async function getDetail(uuid) {
// https://wkhtmltopdf.org/
async function transferToPDF(htmlFilePath, pdfFilePath) {
console.log(`开始转换 ${pdfFilePath}`);
let html = `
<html>
<head>
<meta charset="utf-8">
<style>
* {
font-size: large;
/*
font-size: 30px;
font-size: large !important;
*/
}
img {
max-width: 100%;
}
</style>
</head>
@ -87,8 +96,30 @@ async function transferToPDF(htmlFilePath, pdfFilePath) {
${fs.readFileSync(htmlFilePath)}
</body>
</html>`
wkhtmltopdf(html, { pageSize: "A4" })
// fs.writeFileSync(`./output/test.html`, html);
wkhtmltopdf(html, { pageSize: "A4", minimumFontSize: 10, disableSmartShrinking: true })
.pipe(fs.createWriteStream(pdfFilePath));
}
main();
// ========================================================
// 遍历 output/html 文件夹下的所有html文件
async function transferHTMLToPDF() {
let files = fs.readdirSync("./output/html");
for (let i = 0; i < files.length; i++) {
const fileName = files[i];
if (!fileName.endsWith(".html")) continue; // 过滤掉 html 文件
let pdfFileName = fileName.replace(".html", ".pdf");
await transferToPDF(`./output/html/${fileName}`, `./output/pdf/${pdfFileName}`);
}
console.log("完成");
}
// 爬取 + 转换为pdf
// main();
// 已经爬取过只需要转pdf
transferHTMLToPDF();
// 测试调试用
// transferToPDF(`./output/html/8.3 操作系统(三).html`, `./output/test.pdf`)