【问题标题】:PDF-LIB add text and image from existing PDFPDF-LIB 从现有 PDF 添加文本和图像
【发布时间】:2021-06-16 19:25:25
【问题描述】:

我无法统一多个 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");
}

错误:

error: VM3133:14 Uncaught (in promise) ReferenceError: Cannot access 'pdfDoc' before initialization 在 modifyPdf (:14:29)

【问题讨论】:

    标签: javascript


    【解决方案1】:

    基本上,您在声明之前使用pdfDoc。所以,带上

    const pdfDoc = await PDFDocument.load(existingPdfBytes);
    

    第一次使用前先到顶部。


    最终代码:

    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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2018-06-23
      • 2015-06-23
      • 2018-10-01
      • 2016-02-12
      相关资源
      最近更新 更多