发布
社区首页 >问答首页 >PDF-LIB从现有PDF中添加文本和图像

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

Stack Overflow用户
提问于 2021-06-16 19:25:25
回答 1查看 3.3K关注 0票数 1

我不能得到统一的多个pdf-库的例子为我工作。我想在现有PDF中添加文本和图像的功能相同。它不断地给我带来错误,我不明白为什么。

代码语言:javascript
代码运行次数:0
复制
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");
}

错误:

错误: at 3133:14 ReferenceError:无法在modifyPdf (:14:29)

初始化前访问“pdfDoc”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-16 19:42:45

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

代码语言:javascript
代码运行次数:0
复制
const pdfDoc = await PDFDocument.load(existingPdfBytes);

在第一次使用之前到达顶端。

最后守则:

代码语言:javascript
代码运行次数:0
复制
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");
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68008901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档