pdf-lib.js 未捕获(承诺中)错误:无法解析 PDF 文档(行:0 col:0 偏移=0):找不到 PDF 标头

问题描述 投票:0回答:1

我使用 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();
});
javascript html pdf-lib.js
1个回答
0
投票

好吧,我发现错误是在编辑 PDF 之前集成了 Internet Download Manager IDM 捕获 PDF 并且不允许 PDF 到达 Js 文件

© www.soinside.com 2019 - 2024. All rights reserved.