Skip to content

Commit

Permalink
package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilyes-El-Majouti committed Oct 29, 2023
1 parent e2e89c2 commit 06b5327
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ name: "publish npm"
on: push

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: node
uses: actions/setup-node@v2
with:
node-version: 12
registry-url: https://registry.npmjs.org
- name: publish
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
publish:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: node
uses: actions/setup-node@v2
with:
node-version: 12
registry-url: https://registry.npmjs.org
- name: publish
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
48 changes: 24 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@ const { PDFDocument } = require('pdf-lib');
const sharp = require('sharp');
const ProgressBar = require('progress');

const pdftobuffer = async (pdf, page, progress = false) => {
const pdfcount = await pdftocount(pdf);

assert(Buffer.isBuffer(pdf) || isstream.readable(pdf), 'The pdf must be either a readable stream or a buffer');

if (page !== "all" && !Array.isArray(page)) {
assert((pdfcount - 1) >= page, 'the page does not exist please try again');
assert(typeof page === 'number', `page should be one number, given ${page}`);
assert(Number.isInteger(page), `page should be an integer, given ${page}`);
assert(page >= 0, `the page must be equal to or greater than 0 in the case of ${page}`);
} else if(Array.isArray(page)) {
Array.from(page, (_) => assert((pdfcount - 1) >= _, 'the page does not exist please try again'));
}

const bar = progress ? new ProgressBar('Processing [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 30, total: page === "all" ? pdfcount : 1 }) : null;

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

const imagemagickconverter = async (pdf, page, bar) => {
const imagemagickstream = imagemagick()
.set('density', 200)
Expand Down Expand Up @@ -69,6 +45,30 @@ const imagemagickconverter = async (pdf, page, bar) => {
return resultBuffer;
}

const pdftobuffer = async (pdf, page, progress = false) => {
const pdfcount = await pdftocount(pdf);

assert(Buffer.isBuffer(pdf) || isstream.readable(pdf), 'The pdf must be either a readable stream or a buffer');

if (page !== "all" && !Array.isArray(page)) {
assert((pdfcount - 1) >= page, 'the page does not exist please try again');
assert(typeof page === 'number', `page should be one number, given ${page}`);
assert(Number.isInteger(page), `page should be an integer, given ${page}`);
assert(page >= 0, `the page must be equal to or greater than 0 in the case of ${page}`);
} else if(Array.isArray(page)) {
Array.from(page, (_) => assert((pdfcount - 1) >= _, 'the page does not exist please try again'));
}

const bar = progress ? new ProgressBar('Processing [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 30, total: page === "all" ? pdfcount : 1 }) : null;

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

const pdftocount = async (pdf) => {
const pdfDoc = await PDFDocument.load(pdf);
return pdfDoc.getPageCount();
Expand Down
22 changes: 2 additions & 20 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
const exp = require("constants");
const pdftopic = require("./");
const fs = require("fs");

/*test('converting [email protected] (single file)', async () => {
const pdf = fs.readFileSync('./pdf_files/ilyes.pdf');
const result = await pdftobuffer(pdf, 0);
expect(result).not.toBeNull();
});
test('converting [email protected] (multiple file)', async () => {
const pdf = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');
var results = [];
for (let index = 0; index < 4; index++) {
const result = await pdftobuffer(pdf, index);
results.push(result);
}
expect(results).not.toBeNull();
});*/

/*test('converting [email protected] (single file)', async () => {
test('converting [email protected] (single file)', async () => {
const pdf = fs.readFileSync('./pdf_files/ilyes.pdf');
const result = await pdftopic.pdftobuffer(pdf, 0);
expect(result).not.toBeNull();
Expand All @@ -34,7 +16,7 @@ test('converting [email protected] (multiple file)', async () => {
const pdf = fs.readFileSync('./pdf_files/curriculum_vitae.pdf');
const result = await pdftopic.pdftobuffer(pdf, "all");
expect(result).not.toBeNull();
});*/
});

test('converting [email protected] (all images in one image)', async () => {
const file1 = fs.readFileSync('./curriculum_vitae-converted/curriculum_vitae-0.png');
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "pdftopic",
"version": "1.0.0-alpha",
"version": "1.0.1-alpha",
"description": "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",
"main": "index.js",
"funding":{
"type": "paypal",
"url": "https://www.paypal.me/IlyesElMajouti"
},
"scripts": {
"test": "jest"
},
Expand Down Expand Up @@ -48,7 +52,7 @@
"graphics",
"file conversion"
],
"author": "Ilyes-El-Majouti",
"author": "Ilyes-El-Majouti <[email protected]>",
"license": "ISC",
"bugs": {
"url": "https://github.com/Ilyes-El-Majouti/pdftopic/issues"
Expand All @@ -61,7 +65,6 @@
"pdf-lib": "^1.17.1",
"progress": "^2.0.3",
"sharp": "^0.32.6",
"stream": "^0.0.2",
"stream-to-array": "^2.3.0"
},
"devDependencies": {
Expand Down

0 comments on commit 06b5327

Please sign in to comment.