Estoy usando un módulo Node.js llamado pdf-lib para intentar modificar un PDF cargado en la página web. Cuando subo el PDF al script, aparece un error que dice:
pdf-lib@1.4.0:1 Uncaught (in promise) Error: Failed to parse PDF document (line:0 col:0 offset=0): No PDF header foundAlgunas publicaciones del foro de otros usos con el mismo problema parecían reducirlo a cargar el PDF como una cadena binaria, pero parece que no puedo hacer que funcione porque estoy haciendo todo lo que han dicho para resolver su problema.
Si tuviera una suposición, el error es causado por una de las 2 líneas: fr.readAsBinaryString(file) o var bytes = new Uint8Array(input)
Cualquier ayuda sería muy apreciada. Si usted tiene alguna pregunta, hágamelo saber.
Código:
const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib document.querySelector("input[type=file]").addEventListener("change", function() { file = this.files[0]; fr = new FileReader(); fr.readAsBinaryString(file) fr.onload = function() { modifyPdf(fr.result) }; }); async function modifyPdf(input) { var bytes = new Uint8Array(input); const pdfDoc = await PDFDocument.load(bytes) //below should be irrelevant because it doesn't get past the above line 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!', { x: 5, y: height / 2 + 300, size: 50, font: helveticaFont, color: rgb(0.95, 0.1, 0.1), rotate: degrees(-45), }) const pdfBytes = await pdfDoc.save() download(pdfBytes, "pdf-lib_modification_example.pdf", "application/pdf"); }