What is @types/pdfjs-dist?
@types/pdfjs-dist provides TypeScript definitions for the pdfjs-dist library, which is a popular library for working with PDF files in JavaScript. It allows developers to render PDF documents in web applications, extract text, and interact with PDF content programmatically.
What are @types/pdfjs-dist's main functionalities?
Rendering PDF Pages
This feature allows you to render a PDF page onto an HTML canvas element. The code sample demonstrates how to load a PDF document, get the first page, and render it on a canvas.
const pdfjsLib = require('pdfjs-dist');
const canvas = document.getElementById('pdf-canvas');
const context = canvas.getContext('2d');
pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
pdf.getPage(1).then(function(page) {
const viewport = page.getViewport({ scale: 1.5 });
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
Extracting Text from PDF
This feature allows you to extract text content from a PDF page. The code sample demonstrates how to load a PDF document, get the first page, and extract its text content.
const pdfjsLib = require('pdfjs-dist');
pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
pdf.getPage(1).then(function(page) {
page.getTextContent().then(function(textContent) {
const textItems = textContent.items;
let finalString = '';
for (let i = 0; i < textItems.length; i++) {
finalString += textItems[i].str + ' ';
}
console.log(finalString);
});
});
});
Navigating PDF Pages
This feature allows you to navigate through the pages of a PDF document. The code sample demonstrates how to load a PDF document, render a specific page, and navigate to the next or previous page.
const pdfjsLib = require('pdfjs-dist');
let pdfDoc = null;
let currentPage = 1;
pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
pdfDoc = pdf;
renderPage(currentPage);
});
function renderPage(pageNum) {
pdfDoc.getPage(pageNum).then(function(page) {
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.getElementById('pdf-canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
}
function goToNextPage() {
if (currentPage < pdfDoc.numPages) {
currentPage++;
renderPage(currentPage);
}
}
function goToPreviousPage() {
if (currentPage > 1) {
currentPage--;
renderPage(currentPage);
}
}
Other packages similar to @types/pdfjs-dist
pdf-lib
pdf-lib is a library for creating and modifying PDF documents in JavaScript. Unlike pdfjs-dist, which focuses on rendering and extracting content from existing PDFs, pdf-lib allows you to create new PDFs and modify existing ones programmatically.
pdf2json
pdf2json is a library that converts PDF files to JSON format. It is useful for extracting structured data from PDFs, whereas pdfjs-dist is more focused on rendering and interacting with PDF content in a web application.
pdfkit
pdfkit is a library for generating PDF documents in Node.js. It allows you to create complex PDF documents with text, images, and graphics. While pdfjs-dist is used for rendering and extracting content from PDFs, pdfkit is used for creating new PDF documents.