如何使用jsPDF合并两个PDF文件 [英] How to Merge Two PDF Files Using jsPDF

查看:1492
本文介绍了如何使用jsPDF合并两个PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用jsPDF将两个(或多个)PDF文件合并为一个文件,然后使用浏览器的用户pdf查看器显示该文件.

I'd like to know how to merge two (or more) PDF files into one single file using jsPDF and then show it using the browser's pdf viewer of the user.

我的要求是简化打印一组文档的过程,与其一一打印,不如将它们全部合并并打印一个文件.

My requirement is to make easier the process of printing a group of documents, instead of printing one by one just merge them all and print one single file.

我已经在Stack Overflow中查看了另一个问题,但这并没有太大帮助,因为在这种情况下,他们是从HTML内容创建pdf,在我的情况下,我必须合并一组pdf文件一起. 将使用jsPDF生成的两个PDF合并到一个文档中

I've looked at this other question here in Stack Overflow but that didn't help me too much because in this case they are creating the pdf from HTML content, and in my case i have to merge a set of pdf files together. Merge two PDF's generated with jsPDF into one document

我可以从URL中获取我的PDF文件,也​​可以从它们中获取原始内容(base64代码),所以我想知道是否存在一种使用该库的方法.如果是这样,我只需要知道使用哪种方法,或者可能是指向文档的链接(我也看到过文档,但未找到任何内容)

I could get my PDF files from an url or also getting the raw content from them (the base64 code) so I was wondering if there's a way of using this library to do it. If so I only need to know what methods to use or maybe a link to the documentation (i've also seen documentation but nothing found)

非常感谢您的回答,我们将不胜感激.问候!

Thankyou very much for your answers, any help would be appreciated. Regards!

推荐答案

您可以将jsPDF文档转换为数组缓冲区(在此处检查)

You can turn your jsPDF document into an array buffer (check here)

const doc = new jsPDF('p', 'mm', 'a4');
...
const arrayBuffer = doc.output('arraybuffer');

,然后使用此处给出的解决方案

and then use the solution given here

async mergePdfs(pdfsToMerges: ArrayBuffer[]) {
  const mergedPdf = await PDFDocument.create();
  const actions = pdfsToMerges.map(async pdfBuffer => {
  const pdf = await PDFDocument.load(pdfBuffer);
  const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
  copiedPages.forEach((page) => {
    // console.log('page', page.getWidth(), page.getHeight());
    // page.setWidth(210);
    mergedPdf.addPage(page);
    });
  });
  await Promise.all(actions);
  const mergedPdfFile = await mergedPdf.save();
  return mergedPdfFile;
}

这篇关于如何使用jsPDF合并两个PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆