Last active
October 27, 2025 11:20
-
-
Save ganesh-bruno/e5f5e02485703881bf16f87cf7e3f5e8 to your computer and use it in GitHub Desktop.
pdf2json example using Node VM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const https = require('node:https'); | |
| const PDFParser = require('pdf2json'); | |
| const url = require('node:url'); | |
| const PDF_URL = 'https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf'; | |
| const parsedUrl = url.parse(PDF_URL); | |
| const options = { | |
| hostname: parsedUrl.hostname, | |
| port: parsedUrl.port || 443, | |
| path: parsedUrl.pathname, | |
| method: 'GET' | |
| }; | |
| console.log(options); | |
| const chunks = []; | |
| const request = https.request(options, (response) => { | |
| response.on('data', (chunk) => { | |
| chunks.push(chunk); | |
| }); | |
| response.on('end', async () => { | |
| const buffer = Buffer.concat(chunks); | |
| console.log(buffer.length); | |
| // Create PDFParser instance | |
| const pdfParser = new PDFParser(); | |
| // Set up event handlers | |
| pdfParser.on('pdfParser_dataError', errData => { | |
| console.error('PDF parsing error:', errData.parserError); | |
| }); | |
| pdfParser.on('pdfParser_dataReady', pdfData => { | |
| console.log('PDF parsed successfully'); | |
| // Access text content with: pdfParser.getRawTextContent() | |
| }); | |
| // Parse the buffer - you'll need to write it to a temp file first | |
| // or use an alternative approach | |
| const fs = require('fs'); | |
| const tempFile = './temp.pdf'; | |
| fs.writeFileSync(tempFile, buffer); | |
| pdfParser.loadPDF(tempFile); | |
| // Clean up temp file after parsing | |
| setTimeout(() => { | |
| fs.unlinkSync(tempFile); | |
| }, 1000); | |
| }); | |
| }); | |
| request.on('error', (error) => { | |
| console.error('Request error:', error); | |
| }); | |
| request.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment