1
1
import { Browser } from 'puppeteer' ;
2
2
import { CmsArchiveContentService } from '../cms/CmsArchiveContentService' ;
3
- import { pixelWidthToA4Scale } from './pdf-scale ' ;
4
- import { CmsContentDocument } from '../../../common/cms-documents/content' ;
3
+ import { generatePdfFilename , generatePdfFooter , pixelWidthToA4Scale } from './pdf-utils ' ;
4
+ import { CmsContent } from '../../../common/cms-documents/content' ;
5
5
import archiver from 'archiver' ;
6
6
import { Response } from 'express' ;
7
7
import mime from 'mime' ;
@@ -28,7 +28,7 @@ export class PdfGenerator {
28
28
this . contentService = contentService ;
29
29
}
30
30
31
- public async generatePdfFromVersions (
31
+ public async pdfFromVersionsResponse (
32
32
versionKeys : string [ ] ,
33
33
res : Response ,
34
34
width : number = DEFAULT_WIDTH_PX
@@ -66,7 +66,7 @@ export class PdfGenerator {
66
66
continue ;
67
67
}
68
68
69
- await this . generatePdf ( content . html , width ) . then ( ( pdf ) => {
69
+ await this . generateContentPdf ( content , width ) . then ( ( pdf ) => {
70
70
if ( ! pdf ) {
71
71
return ;
72
72
}
@@ -77,7 +77,7 @@ export class PdfGenerator {
77
77
res . setHeader ( 'Content-Length' , pdf . length * contentVersions . length ) ;
78
78
}
79
79
80
- const fileName = this . getPdfFilename ( content ) ;
80
+ const fileName = generatePdfFilename ( content ) ;
81
81
82
82
archive . append ( pdf , { name : fileName } ) ;
83
83
} ) ;
@@ -95,18 +95,23 @@ export class PdfGenerator {
95
95
return null ;
96
96
}
97
97
98
- const data = await this . generatePdf ( content . html , width ) ;
98
+ const data = await this . generateContentPdf ( content , width ) ;
99
99
if ( ! data ) {
100
100
return null ;
101
101
}
102
102
103
103
return {
104
104
data,
105
- filename : this . getPdfFilename ( content ) ,
105
+ filename : generatePdfFilename ( content ) ,
106
106
} ;
107
107
}
108
108
109
- private async generatePdf ( html : string , width : number ) : Promise < Buffer | null > {
109
+ private async generateContentPdf ( content : CmsContent , width : number ) : Promise < Buffer | null > {
110
+ const { html } = content ;
111
+ if ( ! html ) {
112
+ return null ;
113
+ }
114
+
110
115
const widthActual = width >= MIN_WIDTH_PX ? width : DEFAULT_WIDTH_PX ;
111
116
112
117
// Ensures assets with relative urls are loaded from the correct origin
@@ -124,8 +129,16 @@ export class PdfGenerator {
124
129
125
130
const pdf = await page . pdf ( {
126
131
printBackground : true ,
127
- format : 'a4 ' ,
132
+ format : 'A4 ' ,
128
133
scale : pixelWidthToA4Scale ( widthActual ) ,
134
+ displayHeaderFooter : true ,
135
+ footerTemplate : generatePdfFooter ( content ) ,
136
+ margin : {
137
+ top : '4px' ,
138
+ right : '4px' ,
139
+ bottom : '24px' ,
140
+ left : '4px' ,
141
+ } ,
129
142
} ) ;
130
143
131
144
await page . close ( ) ;
@@ -136,8 +149,4 @@ export class PdfGenerator {
136
149
return null ;
137
150
}
138
151
}
139
-
140
- private getPdfFilename ( content : CmsContentDocument ) {
141
- return `${ content . meta . timestamp } _${ content . name } _${ content . contentKey } -${ content . versionKey } .pdf` ;
142
- }
143
152
}
0 commit comments