我正在一個網頁,使用jquery在document.ready上加載頁面,使用css格式化頁面,使用node.js http從頁面中檢索頁面服務器,html-pdf將html轉換爲pdf文件。從這一點,我將該文件作爲電子郵件附件發送。最終目標是創建發票。問題在於css和js在外部文件中似乎沒有處理。我寧願保留在外部文件,但需要jQuery來填充document.ready和CSS來正確格式化頁面的頁面。另外,我加了cheerio,看看是否能確保處理過程正確發生,並沒有幫助。代碼如下所示:html-pdf:css和js文件似乎沒有被處理
email sending code:
var transporter = nodemailer.createTransport({
service: 'gmail',
host: this.hostname,
auth: {
user: this.smtpusername,
pass: this.smtppassword
}
});
var mailOptions = {
from: fromaddress, // sender address
to: toaddresslist, // list of receivers
subject: subject, // Subject line
html: text
};
if (attachments){
mailOptions.attachments = [
{ // binary buffer as an attachment
filename: 'invoice.pdf',
content: attachments,
contentType: 'application/pdf',
encoding: 'base64'
}
];
}
html conversion code:
let body = [];
var options = {
host: 'localhost',
path: '/invoice/' + invoiceid,
port: '3000'
};
http.request(options, function(response){
response.on('data', function (chunk) {
body.push(chunk);
});
response.on('end', function() {
let buffer = '';
buffer = Buffer.concat(body).toString();
let $ = cheerio.load(buffer);
let newdocument = $.html();
console.log(newdocument);
pdf.create(newdocument, {
directory: "tmp",
format: "letter",
orientation: "portrait",
zoomFactor: ".5", // default is 1
type: "pdf",
quality: "75"
}).toBuffer(function(err, newbuffer){
if (err){
reject(err);
}
if (Buffer.isBuffer(newbuffer)){
resolve(newbuffer);
} else {
reject(new Error('The pdf file could not be generated at this time. Please try again later.'));
}
});
});
})
.end();
當我檢查電子郵件,我得到的PDF格式,但沒有出現與我從API調用期望的,格式化的方式關閉數據填充。爲了測試的目的,我嘗試移動文檔中的css代碼,雖然css仍然關閉,但很明顯,它能夠在此場景中看到css。
1)如何獲取html-pdf包來識別和處理外部css和js文件?
2)html-pdf使用什麼類型的html格式,div或表格?
從您運行html -pdf的位置可以連接到外部文件的鏈接嗎?你使用顯式路徑嗎? – Martina