PDF-LIB 从现有 PDF 添加文本和图像

dia*_*viu 1 javascript

我无法统一多个 pdf-lib 示例来为我工作。我想要执行与在现有 PDF 中添加文本和图像相同的功能。它总是给我错误,我不明白为什么。

const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib

async function modifyPdf() {

  
  // Fetch an existing PDF document
  const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf'
  const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer());

  

  // Embed the Helvetica font
  const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);

  // Get the first page of the document
  const pages = pdfDoc.getPages();
  const firstPage = pages[0];
  
  // Fetch JPEG image
  const jpgUrl = 'https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg';
  const jpgImageBytes = await fetch(jpgUrl).then((res) => res.arrayBuffer());
  
  // Load a PDFDocument from the existing PDF bytes
  const pdfDoc = await PDFDocument.load(existingPdfBytes);
  
  const jpgImage = await pdfDoc.embedJpg(jpgImageBytes);
  const jpgDims = jpgImage.scale(0.25);
  

  // Get the width and height of the first page
  const { width, height } = firstPage.getSize();
    firstPage.drawText('This text was added with JavaScript!', {
       x: 5,
       y: height / 2 + 300,
       size: 50,
       font: helveticaFont,
       color: rgb(0.95, 0.1, 0.1),
       rotate: degrees(-45),
     });

  
        // Add a blank page to the document


 firstPage.drawImage(jpgImage, {
    x: firstPage.getWidth() / 2 - jpgDims.width / 2,
    y: firstPage.getHeight() / 2 - jpgDims.height / 2,
    width: jpgDims.width,
    height: jpgDims.height,
  });
  
  
  // Serialize the PDFDocument to bytes (a Uint8Array)
  const pdfBytes = await pdfDoc.save();

        // Trigger the browser to download the PDF document
  download(pdfBytes, "pdf-lib_modification_example.pdf", "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)

错误:

错误:VM3133:14未捕获(承诺)ReferenceError:在modifyPdf初始化之前无法访问“pdfDoc”(:14:29)

Muh*_*eem 5

基本上,您pdfDoc在声明之前就使用了它。所以,只要带上

const pdfDoc = await PDFDocument.load(existingPdfBytes);
Run Code Online (Sandbox Code Playgroud)

首次使用前请先到顶部。


最终代码:

const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib

async function modifyPdf() {
 
  // Fetch an existing PDF document
  const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf'
  const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer());

  // Load a PDFDocument from the existing PDF bytes
  const pdfDoc = await PDFDocument.load(existingPdfBytes);

  // Embed the Helvetica font
  const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);

  // Get the first page of the document
  const pages = pdfDoc.getPages();
  const firstPage = pages[0];
  
  // Fetch JPEG image
  const jpgUrl = 'https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg';
  const jpgImageBytes = await fetch(jpgUrl).then((res) => res.arrayBuffer());
    
  const jpgImage = await pdfDoc.embedJpg(jpgImageBytes);
  const jpgDims = jpgImage.scale(0.25);
  

  // Get the width and height of the first page
  const { width, height } = firstPage.getSize();
    firstPage.drawText('This text was added with JavaScript!', {
       x: 5,
       y: height / 2 + 300,
       size: 50,
       font: helveticaFont,
       color: rgb(0.95, 0.1, 0.1),
       rotate: degrees(-45),
     });

  
        // Add a blank page to the document


 firstPage.drawImage(jpgImage, {
    x: firstPage.getWidth() / 2 - jpgDims.width / 2,
    y: firstPage.getHeight() / 2 - jpgDims.height / 2,
    width: jpgDims.width,
    height: jpgDims.height,
  });
  
  
  // Serialize the PDFDocument to bytes (a Uint8Array)
  const pdfBytes = await pdfDoc.save();

        // Trigger the browser to download the PDF document
  download(pdfBytes, "pdf-lib_modification_example.pdf", "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)