如何解决使用Socket.io + Puppeteer + Node.js用HTML和CSS创建PDF
我目前正在尝试使用HTML的Node.js在服务器端创建PDF文件,而CSS仍在工作。然后,我想将其附加到我没有找到方法的电子邮件中,但遇到了一个更大的问题。
我是Node.js的新手,我无法使用函数。服务器端有我的代码。
const fs = require('fs');
const puppeteer = require('puppeteer')
const ConnectionPort = 8080;
const path = require('path');
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
io.on('connection',(socket) => {
console.log('a user connected');
socket.on('disconnect',() => {
console.log('user disconnected');
});
socket.on('Creation of PDF Document',(PageHTML,NumberOfTheFile,HeadHTML) => {
console.log('A creation was requested'); // I see that on the console
(async function(){
try{
console.log('Got in the try') // I can't see that on the console
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent('<head>' + HeadHTML + '</head><body>' + PageHTML + '</body>');
await page.emulateMedia('screen');
await page.pdf({
path: path.join(__dirname,'Files',NumberOfTheFile + '.pdf'),format: "Legal",printBackground: true
});
console.log('Creation done!');
await browser.close();
process.exit();
} catch (e)
{
console.log('Error: ',e)
}
})
});
/////////////// Other stuff I was going for that works ////////////////////
socket.on('Asking ID',() => {
console.log('a user is trying to ask for an ID');
var array = fs.readFileSync('ID_Liste.txt').toString().split("\n");
var i = 0;
for(i in array)
{
console.log(array[i]);
}
var ID_BonCommande = parseInt(array[i]) + 1;
fs.appendFile('ID_Liste.txt'," \n \n \n" + "Operation Time : " + "\n" + Date() + " \n\n" + "ID_BonCommande : " + "\n" + ID_BonCommande + '',function (err) {
if (err) throw err;
console.log('ID added : ' + ID_BonCommande);
});
io.emit('Reception ID',ID_BonCommande);
});
////////////////////////////////////////////////////////////////////////////////
});
http.listen(ConnectionPort,() => {
console.log('listening on *: ' +ConnectionPort );
});
这是我在客户端调用函数的方式:
socket.emit('Creation of PDF Document',document.getElementById('Page').innerHTML,document.getElementById(NumberOfThePage).innerHTML,document.getElementsByTagName('head').innerHTML // I need the head for the CSS
);
function AddID()
{
socket.emit('Asking ID');
}
var receive = function(ID_Receved){
document.getElementById('PlaceToPutID').innerHTML = ID_Receved.toString();
}
socket.on('Reception ID',receive)
The Youtube video that helped me
解决方法
我尝试过:
socket.on('Creation of PDF Document',(PageHTML,NumberOfTheFile,HeadHTML) => {
console.log('A creation was requested'); // can't go further
async function Test(){
try{
console.log('Got in the try')
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent('<head>' + HeadHTML + '</head><body>' + PageHTML + '</body>');
await page.emulateMedia('screen');
await page.pdf({
path: path.join(__dirname,'Files',NumberOfTheFile + '.pdf'),format: "Legal",printBackground: true
});
console.log('Creation done!');
} catch (e)
{
console.log('Error: ',e)
}
}
Test();
});
它没有按预期工作...我没有在PDF上获取CSS,但是仍然可以。
我需要CSS才能真正使用它。我们可以这样在客户端使用标签名称样式吗?
document.getElementsByTagName('style').innerHTML
或任何可以将CSS传输到服务器的内容。我的CSS目前处于“样式”标签中,但我不确定这是正确的决定。
我走错了方向?
已编辑*
知道了!
我只是将样式标签放在主体中并能够使用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。