What is pdf-parse?
The pdf-parse npm package is a simple and effective tool for extracting text and metadata from PDF files. It can be used to parse PDF files and retrieve their content in a structured format, making it useful for various applications such as data extraction, text analysis, and document processing.
What are pdf-parse's main functionalities?
Extract Text from PDF
This feature allows you to extract the text content from a PDF file. The code sample reads a PDF file from the filesystem and uses the pdf-parse package to extract and print the text content.
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('example.pdf');
pdf(dataBuffer).then(function(data) {
console.log(data.text); // Print the text content of the PDF
});
Extract Metadata from PDF
This feature allows you to extract metadata from a PDF file. The code sample reads a PDF file from the filesystem and uses the pdf-parse package to extract and print the metadata information such as title, author, and creation date.
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('example.pdf');
pdf(dataBuffer).then(function(data) {
console.log(data.info); // Print the metadata of the PDF
});
Extract Text and Metadata from PDF
This feature allows you to extract both text content and metadata from a PDF file. The code sample reads a PDF file from the filesystem and uses the pdf-parse package to extract and print both the text content and metadata information.
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('example.pdf');
pdf(dataBuffer).then(function(data) {
console.log(data.text); // Print the text content of the PDF
console.log(data.info); // Print the metadata of the PDF
});
Other packages similar to pdf-parse
pdf-lib
pdf-lib is a library for creating and modifying PDF documents in JavaScript. Unlike pdf-parse, which focuses on extracting text and metadata, pdf-lib provides more comprehensive functionality for creating, editing, and manipulating PDF files.
pdfjs-dist
pdfjs-dist is a distribution of the PDF.js library, which is a powerful tool for rendering PDF documents in web applications. While pdf-parse is used for extracting text and metadata, pdfjs-dist is primarily used for rendering PDFs in the browser and provides a more interactive experience.
pdf2json
pdf2json is a node.js module that parses PDF files and converts them into JSON format. It provides similar functionality to pdf-parse in terms of extracting text and metadata, but it outputs the data in a structured JSON format, which can be more convenient for certain applications.
pdf-parse
Pure javascript cross-platform module to extract texts from PDFs.
Similar Packages
Installation
npm install pdf-parse
Basic Usage - Local Files
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer).then(function(data) {
console.log(data.numpages);
console.log(data.numrender);
console.log(data.info);
console.log(data.metadata);
console.log(data.version);
console.log(data.text);
});
Basic Usage - HTTP
You can use crawler-request which uses the pdf-parse
Exception Handling
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer).then(function(data) {
})
.catch(function(error){
})
Extend
function render_page(pageData) {
let render_options = {
normalizeWhitespace: false,
disableCombineTextItems: false
}
return pageData.getTextContent(render_options)
.then(function(textContent) {
let lastY, text = '';
for (let item of textContent.items) {
if (lastY == item.transform[5] || !lastY){
text += item.str;
}
else{
text += '\n' + item.str;
}
lastY = item.transform[5];
}
return text;
});
}
let options = {
pagerender: render_page
}
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer,options).then(function(data) {
});
Options
const DEFAULT_OPTIONS = {
pagerender: render_page,
max: 0,
version: 'v1.10.100'
}
If you need another format except raw text.
max (number)
Max number of page to parse. If the value is less than or equal to 0, parser renders all pages.
version (string, pdf.js version)
check pdf.js
'default'
'v1.9.426'
'v1.10.100'
'v1.10.88'
'v2.0.550'
default uses version v1.10.100
mozilla.github.io/pdf.js
Test
Support
I use this package actively myself, so it has my top priority. You can chat on WhatsApp about any infos, ideas and suggestions.
Submitting an Issue
If you find a bug or a mistake, you can help by submitting an issue to GitLab Repository
Creating a Merge Request
GitLab calls it merge request instead of pull request.
License
MIT licensed and all it's dependencies are MIT or BSD licensed.