pdf - 如何在 Apps 脚本中使用外部 Javascript 库(PDF 库)?

标签 pdf google-apps-script libraries pdf.js pdflib

我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB

我的代码:

eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib/dist/pdf-lib.js").getContentText());

function modifyPdf() {
  const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf'
  const existingPdfBytes = UrlFetchApp.fetch(url).getContentText();

  const pdfDoc = PDFDocument.load(existingPdfBytes)
  const helveticaFont = pdfDoc.embedFont(StandardFonts.Helvetica)

  const pages = pdfDoc.getPages()
  const firstPage = pages[0]
  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),
  })

  const pdfBytes = pdfDoc.save()
}

当我执行功能modifyPDF时,我有:

Erreur  
ReferenceError: PDFDocument is not defined
modifyPdf   @ modifie_pdf.gs:7

您知道如何在我的 Apps 脚本应用程序中导入 js 库吗?

最佳答案

  • eval使用的全局变量命名空间是PDFLib。因此,所有变量(例如 rgblevelsPDFDocument)都是该对象的键,应该这样引用。

  • 库中的大多数函数都使用 promises,虽然应用程序脚本在功能上不支持它,但在语法上却支持它。因此,应该使用asyncawait,否则你只会得到promise对象,而不是实际的document字体

  • 该库使用 setTimeout,这在应用脚本中不可用。我使用 Utilities.sleep 来模拟它的行为。

  • getContentText() 返回文本,而不是二进制内容。使用 getContent() 获取 byte[] 并将其转换为 Uint8Array

eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib/dist/pdf-lib.js").getContentText());
/*+++simulate setTimeout*/setTimeout = (func, sleep) => (Utilities.sleep(sleep),func())

async function modifyPdf() {
  const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf'
  const existingPdfBytes = new /*cast to uint8*/Uint8Array(/*returns byte[]*/UrlFetchApp.fetch(url).getContent/*---Text*/());
  /*+++ simulate import*/const { PDFDocument, StandardFonts, rgb, degrees} = PDFLib;
  const pdfDoc = /*+++*/await PDFDocument.load(existingPdfBytes)
  const helveticaFont = /*+++*/ await pdfDoc.embedFont(StandardFonts.Helvetica)

  const pages = pdfDoc.getPages()
  const firstPage = pages[0]
  const { width, height } = firstPage.getSize()
  firstPage.drawText(`This text was added with JavaScript\n\n${' '.repeat(10)}(Google Apps script)!`, {
    x: width/10 + 60,
    y: height/10 + 120,
    size: 40,
    font: helveticaFont,
    color: rgb(0.1, 0.1, 0.1),
    rotate: degrees(50),
    opacity: 0.5,
  })

  const pdfBytes = /*+++*/await pdfDoc.save();
  /*+++*/DriveApp.createFile(Utilities.newBlob(pdfBytes).setName('newpdf from apps script'))
}

关于pdf - 如何在 Apps 脚本中使用外部 Javascript 库(PDF 库)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73166361/

相关文章:

amazon-web-services - 在 AWS elasticsearch 中索引 pdf 文件

javascript - 谷歌应用脚​​本: pass return value from one function to another

google-apps-script - 捕捉 Google App 脚本配额和限制错误

c++ - 想要在 Web 应用程序中使用 native C 库,我有哪些选择?

import - 如何在dm脚本中导入其他源代码文件

android - PDF 页面导航问题

python - 在Python中保存绘图仅创建一个空的pdf文档

pdf - 如何将 ColdFusion 页面转换为 PDF 下载?

google-apps-script - 如何将格式化文本转换为 Google-apps-script 中的 html 标签?

java - 如何通过Android Studio添加库