admin管理员组

文章数量:1125782

I'm working with NextJS 15 with App router on a monorepo powered by Turbo.

I'm generating a PDF with PDF-Lib but when I'm using drawText() my PDF generation crash

Look my component:

export const CollaborationPdf = () => {
  const [pdfBlobUrl, setPdfBlobUrl] = useState<string | null>(null);

  const generatePdf = async () => {
    try {
      const pdfDoc = await PDFDocument.create();

      const page = pdfDoc.addPage(PageSizes.A4);

      page.drawCircle();
      const { width, height } = page.getSize();

      pdfDoc.addPage().drawText("Hello, World!", {
        x: width / 2,
        y: height / 2,
        size: 30,
        color: rgb(0, 0, 0),
      });


      const pdfBytes = await pdfDoc.save();
      const blob = new Blob([pdfBytes], { type: "application/pdf" });
      const blobUrl = URL.createObjectURL(blob);
      setPdfBlobUrl(blobUrl);
    } catch (error) {
      console.error("Error generating PDF:", error);
    }
  };

  return (
    <div>
      {!pdfBlobUrl && (
        <Button variant="outline" size="sm" onClick={generatePdf}>
          Generate PDF
        </Button>
      )}
      {pdfBlobUrl && (
        <Link href={pdfBlobUrl} target="_blank" rel="noopener noreferrer">
          <Button variant="outline" size="sm">
            View PDF
          </Button>
        </Link>
      )}
    </div>
  );
};

If I use drawCircle() for example, my PDF will be generated as normal But when I use drawText() it crashes

Someone have an idea ?

I'm trying to generate a PDF from scratch with PDF-Lib

本文标签: typescriptError generating PDF invalid distance too far back when using drawText from pdflibStack Overflow