Coder Social home page Coder Social logo

manasmadan / pdf-actions Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 6.0 42 KB

A NPM Package built on top of pdf-lib that provides functonalities like merge, rotate, split,download pdf to disk and many more...

Home Page: https://pdfactions.vercel.app/

License: MIT License

JavaScript 100.00%
pdf pdf-merge pdf-merger react reactjs react-component pdf-split pdf-splitter pdf-processing pdf-lib

pdf-actions's Introduction

Create and modify PDF documents in any JavaScript environment.
Designed to work in any modern JavaScript runtime. Tested in Node, Browser, Deno, and React Native environments.


Table of Contents

Usage Examples

Modify Existing PDFs Get PDFDocument Object

To Modify Esisting PDF Files You Need to First Convert It Into a PDFDocument Object

Empty PDF Document

import { createFileDoc } from "pdf-actions";

const EmptyPDFDocument = await createFileDoc.EmptyPDFDocument();

PDF Document From File

import { createFileDoc } from "pdf-actions";

const PDFDocument = await createFileDoc.PDFDocumentFromFile(file); // file is a HTML File Object from input tag

PDF Document From Unit8Array/ArrayBuffer

import { createFileDoc } from "pdf-actions";

const PDFDocument = await createFileDoc.PDFDocumentFromPDFArray(fileArray); // fileArray is a Unit8Array/ArrayBuffer

PDF Array from HTML File Object

import { pdfArray } from "pdf-actions";

const PDFArray = await pdfArray(file); // file is a HTML File Object from input tag

PDF Array To Blob

import { pdfArrayToBlob } from "pdf-actions";

const PDFBlob = await pdfArrayToBlob(fileArray); // fileArray is a Unit8Array

Merge PDF

import { mergePDF } from "pdf-actions";

const MergedPDFDocument = await mergePDF(filesDocArray); // filesDocArray is a Array of PDFDocument Object

Rotate PDF

import { rotatePDF } from "pdf-actions";

const RotatedPDFDocument = await rotatePDF(fileDoc, degrees);
/*
fileDoc is a PDFDocument Object
degrees is a integer
*/

Split PDF

import { splitPDF } from "pdf-actions";

const SplitPDFDocument = await splitPDF(fileDoc, range, degree);
/*
fileDoc is a PDFDocument Object
range : Page Numbers(both inclusive) is array of two integers - [start,stop]
degree? is a optional parameter and integer
*/

Break Single PDF File Into Multiple PDF

import { breakPDF } from "pdf-actions";

const BreakPDFDocumentsArray = await breakPDF(
  fileDoc,
  pagesInEachFile,
  haveLastPDF,
  degree,
  breakRange
);
/*
fileDoc is a PDFDocument Object
pagesInEachFile is a integer denoting Number of Pages in Each Broken PDF
haveLastPDF is a boolean to whether return the last pdf document irrespective of maxPages
degree? is a optional parameter and integer
breakRange? is a optional paramenter that is a array of two integers [start,stop] that splits the pdf(both inclisive) before breaking it
*/

Flatten PDF Forms

import { flattenPDFForm } from "pdf-actions";

const PDFDocumentWithFormsFlattened = await flattenPDFForm(fileDoc);
/*
fileDoc is a PDFDocument Object
*/

Resize PDF

import { resizePDF } from "pdf-actions";

const PDFDocumentWithFormsFlattened = await resizePDF(
  fileDoc,
  size,
  orientation,
  position,
  degree
);
/*
fileDoc is a PDFDocument Object
size? optional paramenter : string : 2A0 , 4A0 , A[0-10] , B[0-10] , C[0-9] , Executive , Folio , Legal , Letter , RA[0-4] , SR[0-4] , Tabloid 
orientation? optional paramenter : string : Portrait or Landscape
position? optional parameter - Where The Content Should Be : string : Start , Center , End
degree? rotate pdf angle : integer
*/

Add Margin to PDF

import { addMarginPDF } from "pdf-actions";

const AddedMarginPDFDocument = await addMarginPDF(
  fileDoc,
  marginMillimeter,
  degree
);
/*
fileDoc is a PDFDocument Object
marginMillimeter? : optional paramenter : array of length 4
  marginMillimeter[0] -> Left
  marginMillimeter[1] -> Top
  marginMillimeter[2] -> Right
  marginMillimeter[3] -> Bottom
degree? rotate pdf angle : integer
*/

Edit PDF Metadata

import { editMetaData } from "pdf-actions";

const NewMetaDataPDFDocument = await editMetaData(fileDoc, options);
/*
fileDoc is a PDFDocument Object
options = {author,title,creator,keywords,documentCreationDate,documentModificationDate,subject,producer}
*/

Remove PDF Metadata

import { removeMetaData } from "pdf-actions";

const RemovedMetaDataPDFDocument = await removeMetaData(fileDoc);
/*
fileDoc is a PDFDocument Object
*/

Add Page Numbers to PDF

import { addPageNumbers } from "pdf-actions";

const PDFDocumentWithPageNumbers = await addPageNumbers(
  fileDoc,
  pageNumberPosition,
  margin,
  startingPage,
  endingPage,
  startingNumber,
  textSize
);
/*
fileDoc is a PDFDocument Object
pageNumberPosition? : optional : is one of the following string
  - b-l : Bottom Left
  - b-c : Bottom Center : Default
  - b-r : Bottom Right
  - t-l : Top Left
  - t-c : Top Center
  - t-r : Top Right
margin? : optional : is one of the following string
  - Recommended : Default
  - Small
  - Big
startingPage? : optional : is a integer should be >=1 and <=max pages in pdf
endingPage? : optional : is a integer should be >=1 and <=max pages in pdf and >= startingPage
startingNumber? : optional : is a integer denoting the first page number : 1 by default
textSize? : optional 16 by default is a integer
*/

JPG To PDF

import { imageToPDF } from "pdf-actions";

const PDFDocumentFromJPG = await imageToPDF(
  image,
  pageNumberPosition,
  margin,
  startingPage,
  endingPage,
  startingNumber,
  textSize
);
/*
image is a base64 data string
pageSize? : optional : is one of the following string
  - Same as Image
  - 2A0 , 4A0 , A[0-10] , B[0-10] , C[0-9] , Executive , Folio , Legal , Letter , RA[0-4] , SR[0-4] , Tabloid 
pageOrientation? : optional : should pe portrait in case of Same as Image
imagePosition? : optional : should be Start in case of Same as Image
degree? : rotate created PDF
marginMillimeter? : optional paramenter : array of length 4
  marginMillimeter[0] -> Left
  marginMillimeter[1] -> Top
  marginMillimeter[2] -> Right
  marginMillimeter[3] -> Bottom
*/

Installation

NPM Module

To install the latest stable version:

  • With npm
npm install --save pdf-actions
  • With yarn
yarn add pdf-actions

This assumes you're using npm or yarn as your package manager.

Documentation

The repo for the project site (and generated documentation files) is located here: https://github.com/ManasMadan/pdf-actions

License

MIT

pdf-actions's People

Contributors

juanrivlaz avatar manasmadan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pdf-actions's Issues

mergePDF.js: Uncaught (in promise) TypeError: fileDoc.getPageIndices is not a function

Error in mergePDF function, steps to reproduce:

I have an array of Unit8Array elements, where each Unit8Array represents an encoded PDF, then I do:

const files = encodedPdfUnit8Array.map(pdf => {
                    const blob = new Blob([pdf, {type: 'application/pdf'});
                    return new File([blob], "", {lastModified: new Date().getTime(), type: blob.type});
                });

// and then I do
const mergePDFHandler = async (files) => {
         // note the missing async after the forEach loop which is not in the docs.
        files.forEach(async (file)=>await createPDF.PDFDocumentFromFile(file))
        const mergedPDFDocument = await mergePDF(files)
        const mergedPdfFile = await mergedPDFDocument.save();
        const pdfBlob = pdfArrayToBlob(mergedPdfFile);
      };

output is:

mergePDF.js:10 Uncaught (in promise) TypeError: fileDoc.getPageIndices is not a function
    at mergePDF (<anonymous>:37507:62)

Am I doing anything wrong here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.