Skip to content

Commit 2e9a207

Browse files
committed
add userAgent and base64
1 parent 41b9bf8 commit 2e9a207

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf
6464
| 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`. |
6565
| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. |
6666
| 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). |
6769

6870
## Author
6971

src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export default class Config {
3535
readonly scale: number = 1;
3636
readonly landscape: boolean = false;
3737
readonly margin: Margin = new Margin();
38+
readonly userAgent?: string;
39+
readonly base64: boolean = false;
3840

3941
constructor(params?: { [key: string]: any }) {
4042
for (const param in params) {
@@ -142,6 +144,19 @@ export default class Config {
142144
);
143145
}
144146
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;
145160
}
146161
}
147162
}

src/generator/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (process.env.HTML2PDF_NO_SANDBOX) {
1212
args.push("--no-sandbox");
1313
}
1414

15-
export default async (config: Config) => {
15+
export default async (config: Config): Promise<Buffer> => {
1616
// Create a browser instance
1717
const browser = await puppeteer.launch({
1818
headless: true,
@@ -31,6 +31,11 @@ export default async (config: Config) => {
3131
// Set CSS media type
3232
await page.emulateMediaType(config.mediaType);
3333

34+
if (config.userAgent) {
35+
// Set user-agent
36+
await page.setUserAgent(config.userAgent);
37+
}
38+
3439
if (config.isUrl) {
3540
// https://pptr.dev/api/puppeteer.page.goto
3641
await page.goto(config.source, { waitUntil: "networkidle0" });

src/rest-api/generate.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export default async (req: Request, res: Response) => {
3333

3434
try {
3535
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+
3643
res.set("Content-Type", "application/pdf");
3744
res.status(200).send(pdf);
3845
return;

0 commit comments

Comments
 (0)