我正在尝试将pdfkit生成的pdf文件作为输入发送到pdflib进行合并。我正在使用异步函数。我的项目是使用sails Js版本开发的:“^1.2.3”,"node":"^12.16",我的pdf-kit版本是:"^0.11.0","pdf-lib":"^1.9.0",代码如下:
const textbytes=fs.readFileSync(textfile);
var bytes1 = new Uint8Array(textbytes);
const textdoc = await PDFDocumentFactory.load(bytes1)我得到的错误是:
UnhandledPromiseRejectionWarning:错误:无法解析PDF文档(行:0列:0 offset=0):找不到PDF头
请帮我解决这个问题。
发布于 2020-12-09 16:09:01
你真的不需要这句话。
var bytes1 = new Uint8Array(textbytes);只需读取文件并在参数中发送textbytes就足够了。
我使用这个函数来合并一个pdfBytes数组来生成一个大的PDF文件:
async function mergePdfs(pdfsToMerge)
{
const mergedPdf = await pdf.PDFDocument.create();
for (const pdfCopyDoc of pdfsToMerge)
{
const pdfDoc = await pdf.PDFDocument.load(pdfCopyDoc);
const copiedPages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices());
copiedPages.forEach((page) => {
mergedPdf.addPage(page);
});
}
const mergedPdfFile = await mergedPdf.save();
return mergedPdfFile;
};所以基本上在你添加了函数mergePdfs(pdfsToMerge)之后,你可以像这样使用它:
const textbytes = fs.readFileSync(textfile);
const textdoc = await PDFDocumentFactory.load(bytes1)
let finalPdf = await mergePdfs(textdoc);https://stackoverflow.com/questions/63305049
复制相似问题