PDF.js compiled for node-less, serverless enviroments.

Overview

Pdf.mjs

License: MIT Types npm npm bundle size

PDF.js custom-compiled for nodeless, serverless enviroments, like Deno Deploy and Cloudflare Workers. Rocking in under 700kb uncompressed.

Usage

🦕 Deno

import { getDocument } from 'https://esm.sh/pdf.mjs'

const data = Deno.readFileSync('./file.pdf')
const doc = await getDocument(data).promise

console.log(await doc.getMetadata())

for (let i = 1; i <= doc.numPages; i++) {
    const page = await doc.getPage(i)
    const textContent = await page.getTextContent()
    const contents = textContent.items.map((item) => item.str).join(' ')
    console.log(contents)
}

⚡️ Cloudflare Workers

Installation

npm i pdf.mjs
# or
yarn add pdf.mjs

Compatibility flag

Make sure you enable the "streams_enable_constructors" compatibility flag in your wrangler.toml file:

compatibility_flags = [ "streams_enable_constructors" ]

Example

import { getDocument } from 'pdf.mjs'

export default {
    async fetch(request) {
        const url = new URL(request.url).searchParams.get('pdf')

        if(!url)
            return new Response('400: Bad request',{
                status: 400
            })

        // Fetch the pdf file, as binary
        const buff = await fetch(url).then(r => r.arrayBuffer())

        // Note: passing an url to getDocument is not supported on workers
        const doc = await getDocument(buff).promise

        const meta = await doc.getMetadata()
        const pages = []

        for (let i = 1; i <= doc.numPages; i++) {
            const page = await doc.getPage(i)
            const textContent = await page.getTextContent()
            const contents = textContent.items.map((item) => item.str).join(' ')
            pages.push(contents)
        }

        return new Response(JSON.stringify({
            meta,
            pages
        }, null, 2), {
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }
}

How?

Two things:

  • By removing all polyfills, & some rendering logic bundled into PDF.js
  • Using an older version of PDF.js (v2.0.943), without XFA form support, saving about 300kb
You might also like...

A Serverless GraphQL Sample project using Apollo and Serverless Framework with TypeScript and Webpack.

Serverless GraphQL Boilerplate This is a base project with a structure that includes Serverless Framework, Apollo, TypeScript and Webpack. It can be d

Aug 23, 2022

AWS Lambda & Serverless - Developer Guide with Hands-on Labs. Develop thousands line of aws lambda functions interact to aws serverless services with real-world hands-on labs

AWS Lambda & Serverless - Developer Guide with Hands-on Labs. Develop thousands line of aws lambda functions interact to aws serverless services with real-world hands-on labs

AWS Lambda & Serverless - Developer Guide with Hands-on Labs UDEMY COURSE WITH DISCOUNTED - Step by Step Development of this Repository - https://www

Dec 17, 2022

The AKE-less General Purpose Build System with JavaScript DSL for Node.js platform.

The AKE-less General Purpose Build System with JavaScript DSL for Node.js platform.

The AKE-less General Purpose Build System with JavaScript DSL for Node.js platform. Inspired by NUKE. This project is reaching a mature stage, althoug

Oct 16, 2022

PDF data extraction for Physiotherapy Board NZ APC's

physioboard-apc-reader Parses and extracts info from NZ Physio board issued certificates Install npm install physioboard-apc-reader Use import { physi

Jan 13, 2022

Better looking pdf for lovely rustacean 3

The Rust Programming Language (TRPL) PDF Better looking pdf in your finger tips. Please find the latest version of pdf in release tab. Contributing I

Dec 29, 2022

Obsidian Extract PDF Annotations Plugin

Obsidian Extract PDF Annotations Plugin

Obsidian Extract PDF Annotations Plugin This is a plugin for Obsidian. It extracts Annotations from PDF Files. Usage This Plugin visits all PDF files

Dec 22, 2022

PDF translation add-on for Zotero 6

PDF translation add-on for Zotero 6

Zotero PDF Translate This is an add-on for Zotero 6. It provides PDF translation for Zotero's built-in PDF reader. Quick Start Guide Install Download

Jan 8, 2023

PDF Preview for Zotero.

PDF Preview for Zotero.

Zotero PDF Preview Preview PDF attachments in the library view. Fast & easy. Do not require any third-party softwares. Quick Start Guide Install Downl

Jan 4, 2023

Wrapper for PDF JS to add annotations

Wrapper for PDF JS to add annotations

FabricJS layer on top of Mozilla's PDFJS to add ANNOTATIONS Features Supports PDFs with multiple pages Free draw tool Add text Add arrows Add rectangl

Jan 6, 2023
Owner
Brúnó Salomon
Student
Brúnó Salomon
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
AWS CDK compiled for web (and Node!)

cdk-web ?? DEMO ?? AWS CDK compiled for web (and Node!) cdk-web and aws-cdk-web are functionally identical packages on npm. read about the differences

Sepehr Laal 44 Dec 5, 2022
Minimal template engine with compiled output for JavaScript.

@fnando/seagull Minimal template engine with compiled output for JavaScript. Installation This package is available as a NPM package. To install it, u

Nando Vieira 5 Mar 1, 2022
Remix sandbox repo for Rust compiled to WASM and to native N-API modules

Rust <-> Remix Sandbox Now with both native Rust and WASM versions! If you want to combine the Web Fundamentals & Modern UX of Remix together with the

Ben Wishovich 26 Dec 30, 2022
Solid, AssemblyScriptified and compiled to WebAssembly

solid-as Solid as _____! Solid, AssemblyScriptified and compiled to WebAssembly (very work in progress). Installation TODO Test npm i && npm test TODO

Joe Pea 4 Jul 19, 2022
A compiled-away, type-safe, readable RegExp alternative

?? magic-regexp A compiled-away, type-safe, readable RegExp alternative ✨ Changelog ?? Documentation ▶️ Online playground Features Runtime is zero-dep

Daniel Roe 1.5k Jan 8, 2023
A library of icons rendered purely in CSS and compiled using SASS.

Welcome to PureIcons.css: This is a library of icons rendered purely in CSS and compiled using SASSs. It is currently at 79 icons. More will be added

William Troup 2 Apr 27, 2021
Learn Web 2.0 and Web 3.0 Development using Next.js, Typescript, AWS CDK, AWS Serverless, Ethereum and AWS Aurora Serverless

Learn Web 2.0 Cloud and Web 3.0 Development in Baby Steps In this course repo we will learn Web 2.0 cloud development using the latest state of the ar

Panacloud Multi-Cloud Internet-Scale Modern Global Apps 89 Jan 3, 2023