bpampuch / pdfmake

Client/server side PDF printing in pure JavaScript
http://pdfmake.org
Other
11.63k stars 2.04k forks source link

bugged height in sizePage / Page size is not applying on second page #2551

Open calldeludo opened 1 year ago

calldeludo commented 1 year ago

I am creating some simple text pdf and each document has a dynamic height... so i am using pageSize: { width: 400, height: 'auto } but after pageBreak, the height is not fixed to the end of the text anymore... is that a bug?

image

If it has any solution i would really apreciate your help. thanks in advance

liborm85 commented 1 year ago

auto page was designed to be used for continuous paper. It doesn't make sense to generate pages with different sizes of each page according to the size of the content. Do you have any real use case for this?

calldeludo commented 1 year ago

well... im printing some shipping labels that can income with different height, at this moment im generating multiples pdf and merging all with pdf-lib... is a solution for me... but i detected this behivor and i was wondering if there is any option to fix it that im missing... i can understand that is has no sense and is done like this on porpouse

Here i let my code maybe it helps someone...


  async mergeBase64PDFs(base64PDFs: string[]) {
    const mergedPDF = await PDFDocument.create();
    const pdfDocs = [];

    // Itera sobre cada archivo PDF y agrega sus páginas al documento combinado
    for (let i = 0; i < base64PDFs.length; i++) {
      const base64PDF = base64PDFs[i];
      const pdfData = atob(base64PDF);
      const pdfDoc = await PDFDocument.load(new Uint8Array([...pdfData].map(char => char.charCodeAt(0))));
      pdfDocs.push(pdfDoc);
      const pages = await mergedPDF.copyPages(pdfDoc, pdfDoc.getPageIndices());
      pages.forEach((page: PDFPage) => mergedPDF.addPage(page));
    }
    // Abre el archivo combinado en una nueva ventana del navegador
    const mergedPDFData = await mergedPDF.save();
    var mergedPDFBase64 = btoa(String.fromCharCode(...mergedPDFData));

    this.openPdf(mergedPDFBase64)
  }
  openPdf = (basePdf) => {
    let byteCharacters = atob(basePdf);
    let byteNumbers = new Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
      byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    let byteArray = new Uint8Array(byteNumbers);
    let file = new Blob([byteArray], { type: 'application/pdf;base64' });
    let fileURL = URL.createObjectURL(file);
    window.open(fileURL);
  }

Push all the pdfs as base64 and then use these functions should works :)