foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.95k stars 1.16k forks source link

feeding pdf2json data in pdfkit #1151

Closed SiriSuri closed 3 years ago

SiriSuri commented 4 years ago

Hi.. I am trying to read a pdf file using pdf2json and feed the same text to pdfkit using doc.text(). But its showing errors. Can someone help me:

This is my code. var PDFParser = require("pdf2json"); let pdfParser = new PDFParser(this,1); pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) ); pdfParser.on("pdfParser_dataReady", pdfData => { pdfParser.getRawTextContent(); doc.text(pdfParser.getRawTextContent()) }) pdfParser.loadPDF('./assets/images/logs/'+dashboard[d].dashboard[0].loglocation);

error i am getting is: throw er; // Unhandled 'error' event ^ Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF at readableAddChunk (_stream_readable.js:259:30) at PDFDocument.Readable.push (_stream_readable.js:212:10)

I used pdfreader also to read the data. But it is also showing the same error.

Can someone help me please. I am using sails js version:1.2.3 node version:12.16 pdf kit:0.11.0

blikblum commented 4 years ago

Can you create a sample example?

No need to use pdf2json, just call to doc.text with the return of pdfParser.getRawTextContent() that is giving problem

SiriSuri commented 4 years ago

Sorry.. I didnt understand. Can you be clear.. without using pdf2json how can i read the contents of a pdf?

blikblum commented 4 years ago

Just to make a simpler test case. Run your program and copy the return value of pdfParser.getRawTextContent().

Create a pdfkit demo that calls doc with a string with the copied value

SiriSuri commented 4 years ago

I am trying to generate the sample now. My requirement is so complex. I am reading some values from database around 100 fields and some PDF files also. I am generating a pdf using pdfkit. Now i want to paste the content of read pdf files in the document using pdfkit. for this i am using pdf2json. is there any other alternative?

SiriSuri commented 4 years ago

This is the sample I tried:

let doc = new PDFDocument({ size: "A4", margin: 50 },{compress:false}); let pdfParser = new PDFParser(this,1); pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) ); pdfParser.on("pdfParser_dataReady", pdfData => { pdfParser.getRawTextContent(); doc.text(pdfParser.getRawTextContent())}); pdfParser.loadPDF('./assets/images/logs/1.pdf'); doc.end(); doc.pipe(fs.createWriteStream('./test.pdf')); return doc;

The error i got is: events.js:292 throw er; // Unhandled 'error' event ^

Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF at readableAddChunk (_stream_readable.js:259:30) at PDFDocument.Readable.push (_stream_readable.js:212:10) at PDFDocument._write (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:5444:10) at PDFReference.finalize (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:240:19) at PDFReference.end (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:219:17) at PDFPage.end (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:399:21) at PDFDocument.flushPages (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:5395:12) at PDFDocument.addPage (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:5347:12) at LineWrapper.nextSection (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3656:21) at D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3598:39 at LineWrapper.eachWord (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3485:26) at LineWrapper.wrap (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3543:10) at PDFDocument._text (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3734:15) at PDFDocument.text (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\node_modules\pdfkit\js\pdfkit.js:3745:17) at PdfParser. (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\api\controllers\DashboardController.js:45:9) at PdfParser.emit (events.js:315:20) Emitted 'error' event on PDFDocument instance at: at errorOrDestroy (internal/streams/destroy.js:108:12) at readableAddChunk (_stream_readable.js:259:7) at PDFDocument.Readable.push (_stream_readable.js:212:10) [... lines matching original stack trace ...] at PdfParser. (D:\POSOCO\CyberSecurity\cybersecuirtydashboard\api\controllers\DashboardController.js:45:9) { code: 'ERR_STREAM_PUSH_AFTER_EOF' }

blikblum commented 4 years ago

is there any other alternative?

The alternative is what i said, but seems you did not understand. Sorry

SiriSuri commented 4 years ago

sorry. But where am i going wrong? Can you please correct?

blikblum commented 4 years ago

Just run your program without pdfkit. Copy the value returned from pdfParser.getRawTextContent() somewhere (in the clipboard using the debugger or storing in a file).

Create a new pdfkit program that just call doc.text with the string value you just copied