Git Product home page Git Product logo

ilyes-el-majouti / pdftopic Goto Github PK

View Code? Open in Web Editor NEW
26.0 2.0 6.0 3.15 MB

Built for Node.js, this package empowers users to effortlessly convert PDF files into images of exceptional quality, supporting multiple formats including PNG, JPG, GIF, and others. Its streamlined functionality ensures a smooth and reliable conversion process, providing users with the flexibility to obtain top-notch images from their PDF documents

License: Apache License 2.0

JavaScript 100.00%
image jpeg jpg pdf-to-image pdf-to-image-nodejs pdf-to-jpg pdf2png pdftoimage pdftoimages pdftopng

pdftopic's Introduction

PDFTOPIC By Ilyes-El-Majouti

Version-Project CodeFactor

Donate Minified-Size Download-Month License-Project Stars-Project Last-Commit

The pdftopic package offers an advanced solution for converting PDF files into various image formats such as PNG, JPG, etc. With a particular focus on rendering quality, pdftopic distinguishes itself by producing output of exceptional quality, surpassing the performance of other packages available on the market. Designed to meet the critical needs of demanding users, pdftopic effectively and reliably solves the rendering quality problems that many other packages present, offering an unrivalled and reliable PDF conversion solution for developers and IT professionals.

IMPORTANT NOTE: We strongly encourage you to support this library by making a donation via PayPal. Your generosity is highly appreciated, and we also welcome contributors with gratitude!

Prerequisites

  • node >= 12.x
  • imagemagick

Don't have imagemagick yet ?

Follow this guide to install the required dependencies.

Installation

npm install --save pdftopic

Usage

Converting specific page of PDF from buffer, then saving as image file

import pdftopic from "pdftopic";
import fs from "fs";

(async () => {
  const curriculum_vitae = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');

  const converted_result = await pdftopic.pdftobuffer(curriculum_vitae, 0);
  fs.writeFileSync(`./png_files/curriculum_vitae.png`, converted_result[0]);
})();

Converting specific pages of PDF from buffer, then saving as image file

import pdftopic from "pdftopic";
import fs from "fs";

(async () => {
  const curriculum_vitae = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');

  const converted_result = await pdftopic.pdftobuffer(curriculum_vitae, [ 1, 3 ]);
  converted_result.forEach((file, index) => {
    fs.writeFileSync(`./curriculum_vitae-${index}.png`, file);
  });
})();

Converting all pages of PDF from buffer, then saving as image file

import pdftopic from "pdftopic";
import fs from "fs";

(async () => {
  const curriculum_vitae = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');

  const converted_result = await pdftopic.pdftobuffer(curriculum_vitae, "all");
  
  converted_result.forEach((file, index) => {
      fs.writeFileSync(`./png_files/curriculum_vitae-${index}.png`, file);
  });
})();

Converting all pages of PDF from buffer, then concat images and save image file

import pdftopic from "pdftopic";
import fs from "fs";

(async () => {
  const curriculum_vitae = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');

  const converted_result = await pdftopic.pdftobuffer(curriculum_vitae, "all");
  const concat_converted_result = await pdftopic.bufferstoappend(allfiles);

  fs.writeFileSync(`./png_files/curriculum_vitae-big.png`, concat_converted_result);
})();

PDFTOPIC API

pdftobuffer(buffer, page, progress)

Initiate the conversion of a PDF to a buffer by supplying the function with a PDF and a specific page number or 'all' for the entire document. The function validates the input and processes the conversion accordingly, with an optional progress bar to track the process.

Functions

Convert a specific page of the PDF to Image/Buffer by supplying a file path.

pdftobuffer(buffer, page, progress)
  • buffer - buffer pdf file
  • page - page number to convert to an image
  • progress - progress converting.

Converts PDF to Image/Buffer by supplying a file path

pdftobuffer(buffer, pages, progress)
  • buffer - buffer pdf file
  • pages - page numbers to convert to image
    • set pages to "all" to convert all pages
    • pages also accepts an array indicating the page number e.g. [1,2,3]
    • also accepts number e.g. 1
  • progress - progress converting
    • set false to disable progressbar (in default)
    • set true to enable progressbar

bufferstoappend(buffers, progress)

Kick-start the concatenation of multiple buffers by feeding the function with an array of buffers.

Functions

Concatenate multiple buffers into a single buffer by providing an array of buffers to the function. The function processes each buffer, appends them together, and returns the combined buffer.

bufferstoappend(buffers, progress)
  • buffers - array of buffers images

pdftocount(buffer)

Start the process of counting the number of pages in a PDF by providing a PDF buffer to the function.

Functions

Determine the total number of pages in a PDF document by supplying the PDF to the function. The function loads the PDF and returns the page count.

pdftocount(buffer)
  • buffer - buffer pdf file

getDimmentions(buffers)

To use the getDimmentions function, you need to provide an array of buffers as input. Each element of this array must represent an image in buffer form. Make sure that each buffer in the array corresponds to a valid image. Once you've supplied this array of buffers, you can use it as an argument to call the getDimmentions function.

Functions

The getDimmentions function is an asynchronous function that takes an array of buffers as an argument. The function returns an object containing the following information:

  • outputImgWidth: the maximum width of all the images in the array.
  • outputImgHeight: the sum total of the heights of all the images in the array.
  • dimmentions: an array containing the dimensions (width and height) of each image in {width, height} format.
  • channels: the number of elements in the dimensions array, corresponding to the number of images processed.
getDimmentions(buffers)
  • buffers - array of buffers images

New updates

[ADDITION] TypeScript Support Added

In line with the latest update, version [email protected] now includes TypeScript support. This addition enables developers to harness the benefits of TypeScript within their projects, enhancing code quality, readability, and maintainability. With TypeScript support, users can leverage static typing, improved code intelligence, and a more robust development experience.

[UPDATE] Simple file conversion

Comparison between old and new versions As part of this update, a comparative evaluation of conversion speed was carried out on a single file between version [email protected] and the latest version [email protected]. This analysis revealed a significant improvement in conversion performance in the latest version, demonstrating a clear acceleration in the conversion process.

[UPDATE] Multiple file conversion

Comparison between old and new versions In line with this update, a comparative analysis of conversion speed was carried out between the previous version [email protected] and the current version [email protected], based on the processing of several files. This evaluation demonstrated a significant improvement in conversion performance in the more recent version, highlighting a clear acceleration in the conversion process.

[ADDITION] Concatenate multiple images into a single visual entity

Concatenate multiple images In line with this update, the current version of [email protected] has been enhanced by the integration of an image merge feature, enabling multiple images to be combined into a single visual entity. Although this feature is still in the beta phase, we are currently working on an improved beta version, offering even faster merging speeds than those currently available.

pdftopic's People

Contributors

ilyes-el-majouti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pdftopic's Issues

Too many magick processes

Hi

Tool works great for my use-case. Thanks!

Got a problem though.
For big PDF documents this code

const promises = Array.from(Array.isArray(page) ? page : { length: pdfcount }, (_, index) => imagemagickconverter(pdf, Array.isArray(page) ? _ : index, progress ? bar : null));
return Promise.all(promises);

is pretty much killing my system by hundreds of magick processes.

Would be nice to use something like p-limit to make it possible to limit number of processes.

Error: write EOF from ImageMagick

Ignore the error in ImageMagick.:
������ - 200

Error:
Error: write EOF from ImageMagick
\projects\middleearth_tax_new\node_modules_imagemagick-stream@4.1.1@imagemagick-stream\index.js:274
if (!this.listeners('error')) throw err;
^

TypeError: this.listeners is not a function
at Object.onerror (\projects\middleearth_tax_new\node_modules_imagemagick-stream@4.1.1@imagemagick-stream\index.js:274:16
)
at imagemagickstream.onerror (\projects\middleearth_tax_new\node_modules_pdftopic@1.0.1-alpha@pdftopic\index.js:25:17)
at Socket.emit (node:events:526:35)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v18.17.0

Process finished with exit code 1

ImageMagick Error (Resolved)

Docker FROM node:16-slim

Error Message:
Ignore the error in ImageMagick.: convert-im6.q16: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/408.
convert-im6.q16: no images defined png:-' @ error/convert.c/ConvertImageCommand/3258.

how to fix.
Thanks.

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.