我正在尝试使用jsPDF解决以下问题:
我有一个存储在服务器上的PDF文件。我正在使用jsPDF生成另一个PDF,并尝试将其附加到已存在的PDF文件(如上所述)作为另一页。
我搜索了一下,但找不到任何帮助。我还在stackoverflow上找到了这个问题,但情况不同 - 将现有的Pdf附加到Jspdf
我该如何使其正常工作?还是有其他插件或其他方法可以实现此功能?
我正在尝试使用jsPDF解决以下问题:
我有一个存储在服务器上的PDF文件。我正在使用jsPDF生成另一个PDF,并尝试将其附加到已存在的PDF文件(如上所述)作为另一页。
我搜索了一下,但找不到任何帮助。我还在stackoverflow上找到了这个问题,但情况不同 - 将现有的Pdf附加到Jspdf
我该如何使其正常工作?还是有其他插件或其他方法可以实现此功能?
更新
现在是2020年7月,jsPDF不支持该功能。但是一些用户创建了pull request来添加(复制)来自同一PDF文档的页面。在此链接中,您可以找到如何使用他的函数的示例代码。但是它不能添加来自其他PDF的页面。您可以在这里找到他的函数代码。
您可以使用JavaScript "PDF-lib"实现此功能。源代码和其他信息可以在GitHub页面上找到。您可以在其项目页面上找到此库的许多演示。
"PDF-lib" 可以在任何 JavaScript 环境中创建和修改 PDF 文档。它被设计用于在任何现代 JavaScript 运行时环境下工作,已在 Node.JS、浏览器、Deno 和 React Native 环境中进行了测试。const { PDFDocument } = PDFLib;
async function embedPdfPages()
{
// Fetch American flag PDF
const flagUrl = 'https://pdf-lib.js.org/assets/american_flag.pdf',
// Fetch U.S. constitution PDF
constitutionUrl = 'https://pdf-lib.js.org/assets/us_constitution.pdf',
flagPdfBytes = await fetch(flagUrl).then((res) => res.arrayBuffer()),
constitutionPdfBytes = await fetch(constitutionUrl).then((res) => res.arrayBuffer()),
// Create a new PDFDocument
pdfDoc = await PDFDocument.create();
// Add a blank page to the document
var page = pdfDoc.addPage();
// Embed the first page of the American flag PDF
const [americanFlag] = await pdfDoc.embedPdf(flagPdfBytes),
// Load the constitution PDF into a PDFDocument
usConstitutionPdf = await PDFDocument.load(constitutionPdfBytes),
// Embed the first page of the constitution
firstPageOfConstitution = await pdfDoc.embedPage(usConstitutionPdf.getPages()[0]);
// Draw the American flag page
page.drawPage(americanFlag);
//add a blank new page to the document
page = pdfDoc.addPage();
// Draw the first page of the constitution
page.drawPage(firstPageOfConstitution);
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save();
// Trigger the browser to download the PDF document
download(pdfBytes, "pdf-lib_pdf_page_embedding_example.pdf", "application/pdf");
}
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
p {
font-family: helvetica;
font-size: 24px;
text-align: center;
margin: 25px;
}
.small {
font-family: helvetica;
font-size: 18px;
text-align: center;
margin: 25px;
}
button {
background-color: #008CBA;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
font-size: 16px;
}
<script src="https://unpkg.com/pdf-lib@1.4.0"></script>
<script src="https://unpkg.com/downloadjs@1.4.7"></script>
<p>Click the button to embed PDF pages with <code>pdf-lib</code></p>
<button onclick="embedPdfPages()">Create PDF</button>
<p class="small">(Your browser will download the resulting file)</p>
const { PDFDocument, rgb } = PDFLib
async function addAttachments()
{
// Define attachment URLs
const jpgUrl = 'https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg',
pdfUrl = 'https://pdf-lib.js.org/assets/us_constitution.pdf',
// Fetch attachments
jpgAttachmentBytes = await fetch(jpgUrl).then(res => res.arrayBuffer()),
pdfAttachmentBytes = await fetch(pdfUrl).then(res => res.arrayBuffer()),
pdfDoc = await PDFDocument.create();
// Add the JPG attachment
await pdfDoc.attach(jpgAttachmentBytes, 'cat_riding_unicorn.jpg',
{
mimeType: 'image/jpeg',
description: 'Cool cat riding a unicorn!',
creationDate: new Date('2019/12/01'),
modificationDate: new Date('2020/04/19')
});
// Add the PDF attachment
await pdfDoc.attach(pdfAttachmentBytes, 'us_constitution.pdf',
{
mimeType: 'application/pdf',
description: 'Constitution of the United States',
creationDate: new Date('1787/09/17'),
modificationDate: new Date('1992/05/07')
});
// Add a page with some text
const page = pdfDoc.addPage();
page.drawText('This PDF has two attachments. Note that only some appropriated PDF readers can view attachments. For example the Adobe Reader.', {x: 135, y: 415});
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save();
// Trigger the browser to download the PDF document
download(pdfBytes, "pdf-lib_add_attachments.pdf", "application/pdf");
}
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
p {
font-family: helvetica;
font-size: 24px;
text-align: center;
margin: 25px;
}
.small {
font-family: helvetica;
font-size: 18px;
text-align: center;
margin: 25px;
}
button {
background-color: #008CBA;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
font-size: 16px;
}
blockquote
{
background-color: rgba(255,229,100,.3);
border-left: 8px solid #ffe564;
padding: 15px 30px 15px 15px;
}
<script src="https://unpkg.com/pdf-lib@1.7.0"></script>
<script src="https://unpkg.com/downloadjs@1.4.7"></script>
<br><br><br>
<p>Click the button below to create a document and attach a JPEG image and PDF file with <code>pdf-lib</code></p>
<blockquote>Note that only some PDF readers can view attachments. This includes Adobe Reader, Foxit Reader, and Firefox.</blockquote>
<button onclick="addAttachments()">Create PDF</button>
<p class="small">(Your browser will download the resulting file)</p>
有用的链接:
async function loadPdf(){
const response = await fetch('existing.pdf');
const buffer = await response.arrayBuffer();
const existingPdfDocBytes = new Uint8Array(buffer);
const pdfDoc = PDFLib.PDFDocumentFactory.load(existingPdfDocBytes);
return pdfDoc;
}
doc.output('blob')
上获取jspdf的blob url,然后从您的服务器获取文件并将其转换为blob,然后合并它们。