bpampuch / pdfmake

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

[Question] pdfinfo returning Syntax Error: Illegal character #2577

Closed LeoMouyna closed 1 year ago

LeoMouyna commented 1 year ago

Thank you for this wonderful library ! :pray:

I'm trying to use it (v0.2) to generate pdf server side.

I took a look at the playground server.js code and used a very similar code to generate base64 encoded pdf url. It display well in all browser when pasting it and I can download a well formatted pdf from it.

  render(tasks: Task[], volunteer: Volunteer) {
    const pdfContent = this.generateContent(tasks);
    const header = this.generateHeader(volunteer);
    const footer = this.generateFooter();
    const info = this.generateMetadata(volunteer);

    const pdf = this.printer.createPdfKitDocument({
      info,
      header,
      footer,
      content: pdfContent,
      defaultStyle: { fontSize: 10 },
      styles: this.pdfStyles,
      pageMargins: [40, 80, 40, 80],
    });

    const chunks = [];
    return new Promise((resolve, reject) => {
      pdf.on('data', function (chunk) {
        chunks.push(chunk);
      });
      pdf.on('end', function () {
        const result = Buffer.concat(chunks);
        const base64Content = result.toString('base64');
        const encodedContent = `data:application/pdf;base64,${base64Content}`;
        resolve(encodedContent);
      });
      pdf.on('err', function (error) {
        reject(new PdfException(error));
      });
      pdf.end();
    });
  }

Then I would like to return a file stream without writing a file in my file system. I end up with this kind of code:

  render(tasks: Task[], volunteer: Volunteer) {
    const pdfContent = this.generateContent(tasks);
    const header = this.generateHeader(volunteer);
    const footer = this.generateFooter();
    const info = this.generateMetadata(volunteer);

    const pdf = this.printer.createPdfKitDocument({
      info,
      header,
      footer,
      content: pdfContent,
      defaultStyle: { fontSize: 10 },
      styles: this.pdfStyles,
      pageMargins: [40, 80, 40, 80],
    });

    pdf.end();

    return pdf.pipe(new PassThrough());
  }

But then when I download it, I can't see any content, only blank pages. When I analyze it via pdfinfo I got and error: Syntax Error (318757): Illegal character '>'

I don't think it's related to generateContent because I even tested with "Hello world!" as content and got a blank page with a similar error.

Do you have any clue why a base64 encoded works well and the other proposal doesn't ?

liborm85 commented 1 year ago

Tested with:

var dd = {
    content: [
        'Hello world!'
    ]
}

And pdfinfo show correct output:

pdfinfo document.pdf

Creator:        pdfmake
Producer:       pdfmake
CreationDate:   Sun Apr 30 14:34:20 2023
Tagged:         no
Form:           none
Pages:          1
Encrypted:      no
Page size:      595.28 x 841.89 pts (A4) (rotated 0 degrees)
File size:      6460 bytes
Optimized:      no
PDF version:    1.3

Attach runnable example on playground for reproduce issue.