Skip to content

Commit

Permalink
added support to provide src buf directly as an array
Browse files Browse the repository at this point in the history
  • Loading branch information
coderosh committed Nov 13, 2021
1 parent 5e601fb commit c781cc4
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This package requires image to be of type arraybuffer or uint8array.
const img2 = await fetch('https://img2').then((res) => res.arrayBuffer())

const pdf = await imagesToPDF([
{ src: img1 },
img1,
{ src: img2, options: { height: 234, width: 345 } },
])

Expand All @@ -53,7 +53,7 @@ This package requires image to be of type arraybuffer or uint8array.
const img3 = await fetch('https://img3').then((res) => res.arrayBuffer())
const img4 = await fetch('https://img4').then((res) => res.arrayBuffer())

const imgToPdf = new ImagesToPDF([{ src: img1 }, { src: img2 }])
const imgToPdf = new ImagesToPDF([img1, { src: img2 }])
imgToPdf.addImage(img3)
imgToPdf.addImage(img4, { height: 234, width: 324 })

Expand Down
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
/**
* @type {import("@jest/types").Config.InitialOptions}
*/
const config = {
preset: 'ts-jest/presets/default-esm',
testMatch: ['**/*.test.ts'],
collectCoverage: true,
collectCoverageFrom: ['src/**.ts'],
globals: {
'ts-jest': {
useESM: true,
},
},
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coderosh/images-to-pdf",
"version": "2.0.0",
"version": "2.1.0",
"description": "Combine images to a single pdf on browser and node",
"main": "dist/index.js",
"type": "module",
Expand Down Expand Up @@ -55,6 +55,12 @@
"bugs": {
"url": "https://github.com/coderosh/images-to-pdf/issues"
},
"keywords": [
"pdf",
"images-to-pdf",
"pdf-converter",
"pdf-from-images"
],
"repository": {
"type": "git",
"url": "git+https://github.com/coderosh/images-to-pdf.git"
Expand Down
24 changes: 19 additions & 5 deletions src/ImagesToPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ export interface ImageOptions {
}

class ImagesToPDF {
public images: { src: ArrayBuffer | Uint8Array; options?: ImageOptions }[] =
[]

constructor(
public images: {
src: ArrayBuffer | Uint8Array
options?: ImageOptions
}[] = []
) {}
images: (
| {
src: ArrayBuffer | Uint8Array
options?: ImageOptions
}
| (ArrayBuffer | Uint8Array)
)[] = []
) {
for (const image of images) {
if (image instanceof ArrayBuffer || image instanceof Uint8Array) {
this.images.push({ src: image })
} else {
this.images.push(image)
}
}
}

addImage(img: ArrayBuffer | Uint8Array, options?: ImageOptions) {
this.images.push({ src: img, options })
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import ImagesToPDF, { ImageOptions } from './ImagesToPdf.js'

const imagesToPDF = (
images:
images: (
| {
src: Uint8Array | ArrayBuffer
src: ArrayBuffer | Uint8Array
options?: ImageOptions
}[]
}
| (ArrayBuffer | Uint8Array)
)[]
) => {
const imgToPdf = new ImagesToPDF(images)

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isNum = (a: any) => typeof a === 'number'
const isNum = (a: any) => typeof a === 'number' && a - a === 0

const getOrientation = (width: number, height: number) =>
width > height ? 'l' : 'p'
Expand Down
25 changes: 25 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ describe('imagesToPDF', () => {
`)
})

it('should work for more than one image', async () => {
const imagesToPDF = await import('../src').then((res) => res.imagesToPDF)
const pdf = await imagesToPDF([ab, ab])

expect(pdf).toMatchInlineSnapshot(`
Object {
"arrayBuffer": [Function],
"dataUrl": [Function],
}
`)
})

describe('imagesToPDF:dataUrl', () => {
it('should return data url string', async () => {
const imagesToPDF = await import('../src').then((res) => res.imagesToPDF)
Expand All @@ -53,4 +65,17 @@ describe('imagesToPDF', () => {
expect(isPdf(new Uint8Array(pdf.arrayBuffer()))).toBe(true)
})
})

describe('ImagesToPdf', () => {
test('addImage method should work', async () => {
const ImagesToPdf = await import('../src').then((res) => res.default)
const pdf = new ImagesToPdf()
pdf.addImage(ab, { height: 20, width: 20 })
pdf.addImage(new Uint8Array(ab), { height: 20 })

expect(pdf.images.length).toBe(2)
const arb = (await pdf.createPdf()).arrayBuffer()
expect(isPdf(new Uint8Array(arb))).toBe(true)
})
})
})
29 changes: 29 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isNum, getOrientation } from '../src/utils'

describe('isNum', () => {
it('should return true if given arg is a number', () => {
expect(isNum(5)).toBe(true)
})

it('should return false if given arg is a string', () => {
expect(isNum('2')).toBe(false)
})

it('should return false if given arg is Infinity', () => {
expect(isNum(Infinity)).toBe(false)
})

it('should return false if given arg is NaN', () => {
expect(isNum(NaN)).toBe(false)
})
})

describe('isOrientation', () => {
it("should return 'l' if width is greater than height", () => {
expect(getOrientation(300, 200)).toBe('l')
})

it("should return 'p' if width is less than height", () => {
expect(getOrientation(100, 200)).toBe('p')
})
})

0 comments on commit c781cc4

Please sign in to comment.