Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@types/pdfjs-dist
Advanced tools
@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.
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);
}
}
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 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 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.
npm install --save @types/pdfjs-dist
This package contains type definitions for PDF.js (https://github.com/mozilla/pdf.js).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/pdfjs-dist.
These definitions were written by Josh Baldwin, Dmitrii Sorin, and Kamil Socha.
FAQs
Stub TypeScript definitions entry for pdfjs-dist, which provides its own types definitions
The npm package @types/pdfjs-dist receives a total of 107,759 weekly downloads. As such, @types/pdfjs-dist popularity was classified as popular.
We found that @types/pdfjs-dist demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.