我需要在 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()
}
Run Code Online (Sandbox Code Playgroud)
当我执行该功能时,modifyPDF我有:
Erreur
ReferenceError: PDFDocument …Run Code Online (Sandbox Code Playgroud)