我使用 pdf-lib.js lib,但我遇到了麻烦,这段代码可以工作,但有一天会给我这个错误 注意:pdf在同一条路线中
当我按下按钮时,pdf 会被下载,但没有任何文本,与原始 pdf 相同
<!DOCTYPE html>
<html>
<head>
<title>PDF-lib.js Prueba</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pdf-lib.min.js"></script>
</head>
<body>
<input type="text" name="" id="">
<button id="agregarTexto">Agregar Texto</button>
<script src="pdf.js"></script>
</body>
</html>
document.getElementById('agregarTexto').addEventListener('click', async () => {
const existingPdfBytes = await fetch('Prueba.pdf').then(res => res.arrayBuffer());
const pdfDoc = await PDFLib.PDFDocument.load(existingPdfBytes);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const arialFont = await pdfDoc.embedFont(PDFLib.StandardFonts.Helvetica);
firstPage.drawText('Hola', {
x: 105,
y: 698,
font: arialFont,
size: 11,
color: PDFLib.rgb(0, 0, 0),
});
const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'pruebapdf_modificado.pdf';
a.textContent = 'Descargar PDF Modificado';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
});
好吧,我发现错误是在编辑 PDF 之前集成了 Internet Download Manager IDM 捕获 PDF 并且不允许 PDF 到达 Js 文件