1
1
const path = require ( 'path' )
2
2
const fs = require ( 'fs-extra' )
3
+ const fetch = require ( 'node-fetch' )
3
4
4
5
const PDFMerger = require ( '../index' )
5
6
6
7
const FIXTURES_DIR = path . join ( __dirname , 'fixtures' )
7
8
const TMP_DIR = path . join ( __dirname , 'tmp' )
8
9
9
- jest . setTimeout ( 10000 )
10
+ jest . setTimeout ( 20000 )
10
11
11
12
describe ( 'issues' , ( ) => {
12
13
beforeAll ( async ( ) => {
@@ -31,6 +32,56 @@ describe('issues', () => {
31
32
await merger . save ( path . join ( TMP_DIR , 'issue-42_merged.pdf' ) )
32
33
} )
33
34
35
+ test ( 'check resulting file size (#31)' , async ( ) => {
36
+ function getFilenameFromUrl ( url ) {
37
+ const oUrl = new URL ( url )
38
+ const filename = path . basename ( oUrl . pathname )
39
+ return filename
40
+ }
41
+
42
+ async function downloadFile ( url ) {
43
+ const targetFile = path . join ( FIXTURES_DIR , getFilenameFromUrl ( url ) )
44
+ try {
45
+ await fs . stat ( targetFile )
46
+ return true
47
+ } catch {
48
+ console . log ( 'Big files not present. Downloading...' )
49
+ }
50
+ const res = await fetch ( url )
51
+ const aBuffer = await res . arrayBuffer ( )
52
+ await fs . writeFile ( targetFile , Buffer . from ( aBuffer ) )
53
+ }
54
+
55
+ // add some big pdf-files it doesn't realy matter witch ones!
56
+ const bigFiles = [
57
+ 'https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_SPM.pdf' ,
58
+ 'https://www.ipcc.ch/report/ar6/wg1/downloads/report/IPCC_AR6_WGI_SPM_final.pdf' ,
59
+ 'https://www.ipcc.ch/site/assets/uploads/sites/3/2019/12/SROCC_FullReport_FINAL.pdf'
60
+ ]
61
+
62
+ // download some big files
63
+ try {
64
+ for ( const url of bigFiles ) {
65
+ await downloadFile ( url )
66
+ await fs . stat ( path . join ( FIXTURES_DIR , getFilenameFromUrl ( url ) ) )
67
+ }
68
+ } catch ( e ) {
69
+ console . warn ( `Could not download some big files to test issue #31. Please ensure the urls are still valid! ${ e } ` )
70
+ }
71
+
72
+ const merger = new PDFMerger ( )
73
+
74
+ for ( const url of bigFiles ) {
75
+ // add the first page of each pdf to the result
76
+ merger . add ( path . join ( FIXTURES_DIR , getFilenameFromUrl ( url ) ) , [ 1 ] )
77
+ }
78
+
79
+ await merger . save ( path . join ( TMP_DIR , 'IPCC.pdf' ) )
80
+
81
+ const stats = await fs . stat ( path . join ( TMP_DIR , 'IPCC.pdf' ) )
82
+ expect ( stats . size ) . toBeLessThan ( 2_000_000 ) // less than 2MB
83
+ } )
84
+
34
85
afterAll ( async ( ) => {
35
86
await fs . remove ( TMP_DIR )
36
87
} )
0 commit comments