Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@types/pdfjs-dist

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/pdfjs-dist

TypeScript definitions for PDF.js

  • 2.1.6
  • ts3.2
  • npm
  • Socket score

Version published
Weekly downloads
132K
increased by12.08%
Maintainers
1
Weekly downloads
 
Created

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

FAQs

Package last updated on 04 Nov 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc