question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PDFkit output is unreadable

See original GitHub issue

Question

I’m trying to generate a pdf in the backend and send it to the mobile app but the result is unreadable data.

Description

My question is what doc.pipe (res) stores in res as an answer, whether it’s a blob or something. If you need to do an application-side decoding or because of this unreadable data

Code sample

` try {

  let doc = new PDFDocument();
  doc.info['Title'] = 'test Document';
  doc.text('Hello Bruno Sousa PDF');
  doc.pipe(res);    
  
  doc.end();      
  res.status(200).send();

} catch (error) {
  console.log("error: ", error);
  res.status(500).send({
    status: "error",      
  }
  );
}

`

response in the application the data arrives in this format: ` %PDF-1.3 %ÿÿÿÿ 7 0 obj << /Type /Page /Parent 1 0 R /MediaBox [0 0 612 792] /Contents 5 0 R /Resources 6 0 R

endobj 6 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 8 0 R

endobj 5 0 obj << /Length 220 /Filter /FlateDecode

stream xœµQ1N�1ìó �€`;öx#�¶@‚‚�)�¢8�¶»‚ÿ7x�$ÄmÃ�$ÒX±¬ñÌDˆóÞHBt¥Ó¹|�ÙôîÆwS(˜0µÚ&£q.·�B¢4–ò|ð†† ��Ç ®l� ƒ·ãLÎtPVnÚ¸Iëú–¨�”™ø…Æc¹�åé/ûÑ«¯[�ïWÞËã^±±a«‰%8��º—Ò¤�¦kδÜÛ1‘×�œÑÑfÒŒ�¯á‘=è×;Y/ ±á=g2Ɯ֬�¶W‰F�:6J2ô�%f¡«W,Èó�Äë&㫯ú�=]‚ú endstream endobj 10 0 obj (PDFKit) endobj 11 0 obj (PDFKit) endobj 12 0 obj (D:20191210115354Z) endobj 9 0 obj << /Producer 10 0 R /Creator 11 0 R /CreationDate 12 0 R

endobj 8 0 obj << /Type /Font /BaseFont /Helvetica /Subtype /Type1 /Encoding /WinAnsiEncoding

endobj 4 0 obj <<

endobj 3 0 obj << /Type /Catalog /Pages 1 0 R /Names 2 0 R

endobj 1 0 obj << /Type /Pages /Count 1 /Kids [7 0 R]

endobj 2 0 obj << /Dests << /Names [ ]

endobj xref 0 13 0000000000 65535 f 0000000841 00000 n 0000000898 00000 n 0000000779 00000 n 0000000758 00000 n 0000000208 00000 n 0000000119 00000 n 0000000015 00000 n 0000000661 00000 n 0000000586 00000 n 0000000500 00000 n 0000000525 00000 n 0000000550 00000 n trailer << /Size 13 /Root 3 0 R /Info 9 0 R /ID [ ]

startxref 945 %%EOF `

Your environment

  • pdfkit version: “^0.11.0”
  • Node version: v10.16.3
  • Browser version (if applicable): react-native “0.59.8”
  • Operating System: Linux

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
unknowndomaincommented, Dec 21, 2019

It won’t save the document on the server using this code as you are not accessing the servers file system, however it should be displaying the page in your web browser?

You’ll need to show more code to be honest and give more context for what you are trying to do, however this is not a bug in PDFKit, but your code, so I’d suggest moving to StackOverflow.

0reactions
codesterLalitcommented, Nov 16, 2022

After using doc.pipie(res) don’t use res.status(200).send();

doc.pipe(res) already send your response to server. You’re trying to send it twice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pdfkit output is garbled - node.js - Stack Overflow
Pdfkit output is garbled ... This is the output I now get on my pdf: ... var PDFDocument = require('pdfkit'); var fs=require('fs'); var...
Read more >
Fix errors when rendering PDF output - makandra cards
You can have PDFKit render a website by simply calling PDFKit.new('http://google.com').to_file('google.pdf') . You can then send the...
Read more >
PdfConverter producing unreadable TIFF output
I have used the latest hot fix of Aspose.Pdf.Kit (5.3.1) and the output TIFF image is generated successfully. Please download this hot fix...
Read more >
PDFKit Guide
PDFDocument instances are readable Node streams. They don't get saved anywhere automatically, but you can call the pipe method to send the output...
Read more >
PDF has garbled text when copy pasting - adobe reader
It will take about a minute to export the document. The results weren't perfect, but got me 80% of the way there and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found