Skip to content

Commit c781cc4

Browse files
committed
added support to provide src buf directly as an array
1 parent 5e601fb commit c781cc4

File tree

8 files changed

+101
-12
lines changed

8 files changed

+101
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This package requires image to be of type arraybuffer or uint8array.
3131
const img2 = await fetch('https://img2').then((res) => res.arrayBuffer())
3232

3333
const pdf = await imagesToPDF([
34-
{ src: img1 },
34+
img1,
3535
{ src: img2, options: { height: 234, width: 345 } },
3636
])
3737

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

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

jest.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
/**
2+
* @type {import("@jest/types").Config.InitialOptions}
3+
*/
14
const config = {
25
preset: 'ts-jest/presets/default-esm',
36
testMatch: ['**/*.test.ts'],
7+
collectCoverage: true,
8+
collectCoverageFrom: ['src/**.ts'],
49
globals: {
510
'ts-jest': {
611
useESM: true,
712
},
813
},
14+
coverageThreshold: {
15+
global: {
16+
branches: 100,
17+
functions: 100,
18+
lines: 100,
19+
statements: 100,
20+
},
21+
},
922
moduleNameMapper: {
1023
'^(\\.{1,2}/.*)\\.js$': '$1',
1124
},

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coderosh/images-to-pdf",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Combine images to a single pdf on browser and node",
55
"main": "dist/index.js",
66
"type": "module",
@@ -55,6 +55,12 @@
5555
"bugs": {
5656
"url": "https://github.com/coderosh/images-to-pdf/issues"
5757
},
58+
"keywords": [
59+
"pdf",
60+
"images-to-pdf",
61+
"pdf-converter",
62+
"pdf-from-images"
63+
],
5864
"repository": {
5965
"type": "git",
6066
"url": "git+https://github.com/coderosh/images-to-pdf.git"

src/ImagesToPdf.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@ export interface ImageOptions {
99
}
1010

1111
class ImagesToPDF {
12+
public images: { src: ArrayBuffer | Uint8Array; options?: ImageOptions }[] =
13+
[]
14+
1215
constructor(
13-
public images: {
14-
src: ArrayBuffer | Uint8Array
15-
options?: ImageOptions
16-
}[] = []
17-
) {}
16+
images: (
17+
| {
18+
src: ArrayBuffer | Uint8Array
19+
options?: ImageOptions
20+
}
21+
| (ArrayBuffer | Uint8Array)
22+
)[] = []
23+
) {
24+
for (const image of images) {
25+
if (image instanceof ArrayBuffer || image instanceof Uint8Array) {
26+
this.images.push({ src: image })
27+
} else {
28+
this.images.push(image)
29+
}
30+
}
31+
}
1832

1933
addImage(img: ArrayBuffer | Uint8Array, options?: ImageOptions) {
2034
this.images.push({ src: img, options })

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import ImagesToPDF, { ImageOptions } from './ImagesToPdf.js'
22

33
const imagesToPDF = (
4-
images:
4+
images: (
55
| {
6-
src: Uint8Array | ArrayBuffer
6+
src: ArrayBuffer | Uint8Array
77
options?: ImageOptions
8-
}[]
8+
}
9+
| (ArrayBuffer | Uint8Array)
10+
)[]
911
) => {
1012
const imgToPdf = new ImagesToPDF(images)
1113

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const isNum = (a: any) => typeof a === 'number'
1+
const isNum = (a: any) => typeof a === 'number' && a - a === 0
22

33
const getOrientation = (width: number, height: number) =>
44
width > height ? 'l' : 'p'

tests/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ describe('imagesToPDF', () => {
3131
`)
3232
})
3333

34+
it('should work for more than one image', async () => {
35+
const imagesToPDF = await import('../src').then((res) => res.imagesToPDF)
36+
const pdf = await imagesToPDF([ab, ab])
37+
38+
expect(pdf).toMatchInlineSnapshot(`
39+
Object {
40+
"arrayBuffer": [Function],
41+
"dataUrl": [Function],
42+
}
43+
`)
44+
})
45+
3446
describe('imagesToPDF:dataUrl', () => {
3547
it('should return data url string', async () => {
3648
const imagesToPDF = await import('../src').then((res) => res.imagesToPDF)
@@ -53,4 +65,17 @@ describe('imagesToPDF', () => {
5365
expect(isPdf(new Uint8Array(pdf.arrayBuffer()))).toBe(true)
5466
})
5567
})
68+
69+
describe('ImagesToPdf', () => {
70+
test('addImage method should work', async () => {
71+
const ImagesToPdf = await import('../src').then((res) => res.default)
72+
const pdf = new ImagesToPdf()
73+
pdf.addImage(ab, { height: 20, width: 20 })
74+
pdf.addImage(new Uint8Array(ab), { height: 20 })
75+
76+
expect(pdf.images.length).toBe(2)
77+
const arb = (await pdf.createPdf()).arrayBuffer()
78+
expect(isPdf(new Uint8Array(arb))).toBe(true)
79+
})
80+
})
5681
})

tests/utils.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { isNum, getOrientation } from '../src/utils'
2+
3+
describe('isNum', () => {
4+
it('should return true if given arg is a number', () => {
5+
expect(isNum(5)).toBe(true)
6+
})
7+
8+
it('should return false if given arg is a string', () => {
9+
expect(isNum('2')).toBe(false)
10+
})
11+
12+
it('should return false if given arg is Infinity', () => {
13+
expect(isNum(Infinity)).toBe(false)
14+
})
15+
16+
it('should return false if given arg is NaN', () => {
17+
expect(isNum(NaN)).toBe(false)
18+
})
19+
})
20+
21+
describe('isOrientation', () => {
22+
it("should return 'l' if width is greater than height", () => {
23+
expect(getOrientation(300, 200)).toBe('l')
24+
})
25+
26+
it("should return 'p' if width is less than height", () => {
27+
expect(getOrientation(100, 200)).toBe('p')
28+
})
29+
})

0 commit comments

Comments
 (0)