diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index f5218ba..c86187a 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -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}} diff --git a/index.js b/index.js index 50fee57..dc284f1 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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(); diff --git a/index.test.js b/index.test.js index 1b3d29b..d10b620 100644 --- a/index.test.js +++ b/index.test.js @@ -1,24 +1,6 @@ -const exp = require("constants"); const pdftopic = require("./"); -const fs = require("fs"); -/*test('converting pdftopic@0.1.3 (single file)', async () => { - const pdf = fs.readFileSync('./pdf_files/ilyes.pdf'); - const result = await pdftobuffer(pdf, 0); - expect(result).not.toBeNull(); -}); - -test('converting pdftopic@0.1.3 (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 pdftopic@1.0.0 (single file)', async () => { +test('converting pdftopic@1.0.0 (single file)', async () => { const pdf = fs.readFileSync('./pdf_files/ilyes.pdf'); const result = await pdftopic.pdftobuffer(pdf, 0); expect(result).not.toBeNull(); @@ -34,7 +16,7 @@ test('converting pdftopic@1.0.0 (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 pdftopic@1.0.0 (all images in one image)', async () => { const file1 = fs.readFileSync('./curriculum_vitae-converted/curriculum_vitae-0.png'); diff --git a/package.json b/package.json index a7212dd..6bc49d1 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -48,7 +52,7 @@ "graphics", "file conversion" ], - "author": "Ilyes-El-Majouti", + "author": "Ilyes-El-Majouti ", "license": "ISC", "bugs": { "url": "https://github.com/Ilyes-El-Majouti/pdftopic/issues" @@ -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": {