File tree Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,8 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf
64
64
| scale | float | Set scale. The value must be greater than or equals to ` 0.1 ` , or lower than or equals to ` 2 ` . Default is ` 1 ` . |
65
65
| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. |
66
66
| margin | string | Set margin(s) for the PDF document. It can be all four margin or specified by the values separated with space. Default is ` 0 ` . |
67
+ | userAgent | string | Set custom user-agent string. |
68
+ | base64 | boolean | Return the PDF as a base64-encoded string (REST API only). |
67
69
68
70
## Author
69
71
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ export default class Config {
35
35
readonly scale : number = 1 ;
36
36
readonly landscape : boolean = false ;
37
37
readonly margin : Margin = new Margin ( ) ;
38
+ readonly userAgent ?: string ;
39
+ readonly base64 : boolean = false ;
38
40
39
41
constructor ( params ?: { [ key : string ] : any } ) {
40
42
for ( const param in params ) {
@@ -142,6 +144,19 @@ export default class Config {
142
144
) ;
143
145
}
144
146
break ;
147
+ case "userAgent" :
148
+ if ( typeof params [ param ] === "string" ) {
149
+ this . userAgent = params [ param ] ;
150
+ }
151
+ break ;
152
+ case "base64" :
153
+ if (
154
+ typeof params [ param ] === "string" ||
155
+ typeof params [ param ] === "boolean"
156
+ ) {
157
+ this . base64 = params [ param ] ;
158
+ }
159
+ break ;
145
160
}
146
161
}
147
162
}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ if (process.env.HTML2PDF_NO_SANDBOX) {
12
12
args . push ( "--no-sandbox" ) ;
13
13
}
14
14
15
- export default async ( config : Config ) => {
15
+ export default async ( config : Config ) : Promise < Buffer > => {
16
16
// Create a browser instance
17
17
const browser = await puppeteer . launch ( {
18
18
headless : true ,
@@ -31,6 +31,11 @@ export default async (config: Config) => {
31
31
// Set CSS media type
32
32
await page . emulateMediaType ( config . mediaType ) ;
33
33
34
+ if ( config . userAgent ) {
35
+ // Set user-agent
36
+ await page . setUserAgent ( config . userAgent ) ;
37
+ }
38
+
34
39
if ( config . isUrl ) {
35
40
// https://pptr.dev/api/puppeteer.page.goto
36
41
await page . goto ( config . source , { waitUntil : "networkidle0" } ) ;
Original file line number Diff line number Diff line change @@ -33,6 +33,13 @@ export default async (req: Request, res: Response) => {
33
33
34
34
try {
35
35
const pdf = await Generator ( config ) ;
36
+
37
+ if ( config . base64 ) {
38
+ res . set ( "Content-Type" , "text/plain" ) ;
39
+ res . status ( 200 ) . send ( Buffer . from ( pdf ) . toString ( "base64" ) ) ;
40
+ return ;
41
+ }
42
+
36
43
res . set ( "Content-Type" , "application/pdf" ) ;
37
44
res . status ( 200 ) . send ( pdf ) ;
38
45
return ;
You can’t perform that action at this time.
0 commit comments