Skip to content

Commit 5b37b27

Browse files
Merge pull request #10 from condorheroblog/feature-urlOrigin
2 parents 6d7c769 + 76156f4 commit 5b37b27

File tree

20 files changed

+4308
-4292
lines changed

20 files changed

+4308
-4292
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ dist
8787
**/.vuepress/.temp/
8888
**/.vuepress/.cache/
8989
**/.vuepress/dist/
90+
*.pdf

packages/vuepress-plugin-export-pdf-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"cac": "^6.7.14",
5656
"envinfo": "^7.8.1",
5757
"fs-extra": "^11.1.1",
58-
"html-export-pdf-cli": "^1.0.2",
58+
"html-export-pdf-cli": "^1.0.3",
5959
"multimatch": "^6.0.0",
6060
"ora": "^6.3.0",
6161
"pdfjs": "^2.4.7",

packages/vuepress-plugin-export-pdf-core/src/utils/generatePdf.ts renamed to packages/vuepress-plugin-export-pdf-core/src/generatePdf.ts

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { join } from "node:path";
22
import fse from "fs-extra";
33
import pc from "picocolors";
4-
import { Printer, createProgress, writeFileSafe } from "html-export-pdf-cli";
4+
import { Printer, createProgress, isValidUrl, writeFileSafe } from "html-export-pdf-cli";
55
import type { LaunchOptions, PDFOptions } from "html-export-pdf-cli";
66

7-
import { filterRoute } from "../utils";
8-
import type { Page } from "../";
9-
import { mergePDF } from "./mergePDF";
7+
import { filterRoute } from "./utils";
8+
import type { Page } from ".";
9+
import { mergePDF } from ".";
1010

1111
export type UserSorter = (a: Page, b: Page) => number;
1212

@@ -22,6 +22,7 @@ export interface IGeneratePdfOptions {
2222
puppeteerLaunchOptions?: LaunchOptions
2323
pdfOptions?: PDFOptions
2424
pdfOutlines?: boolean
25+
urlOrigin?: string
2526
}
2627

2728
/**
@@ -36,10 +37,12 @@ export const generatePdf = async ({
3637
sorter,
3738
outFile,
3839
outDir,
40+
urlOrigin,
3941
pdfOptions,
4042
pdfOutlines,
4143
routePatterns,
4244
puppeteerLaunchOptions,
45+
// eslint-disable-next-line sonarjs/cognitive-complexity
4346
}: IGeneratePdfOptions) => {
4447
const tempPdfDir = join(tempDir, "pdf");
4548
fse.ensureDirSync(tempPdfDir);
@@ -49,21 +52,62 @@ export const generatePdf = async ({
4952
if (typeof sorter === "function")
5053
exportPages = exportPages.sort(sorter);
5154

55+
const isValidUrlOrigin = isValidUrl(urlOrigin ?? "");
56+
if (urlOrigin && !isValidUrlOrigin) {
57+
process.stdout.write(pc.red(`${urlOrigin} is not a valid URL`));
58+
process.exit(1);
59+
}
60+
5261
const normalizePages = exportPages.map((page) => {
5362
return {
5463
url: page.path,
5564
title: page.title,
56-
location: `http://${host}:${port}${page.path}`,
65+
location: urlOrigin ? `${new URL(urlOrigin).origin}${page.path}` : `http://${host}:${port}${page.path}`,
5766
pagePath: `${tempPdfDir}/${page.key}.pdf`,
5867
};
5968
});
6069

6170
const singleBar = createProgress();
6271
singleBar.start(normalizePages.length);
6372

64-
const printer = new Printer(puppeteerLaunchOptions);
73+
const printer = new Printer();
74+
await printer.setup(puppeteerLaunchOptions);
6575

6676
for (const { location, pagePath, title } of normalizePages) {
77+
const page = await printer.createNewPage(location);
78+
79+
if (urlOrigin && isValidUrlOrigin) {
80+
await page.setRequestInterception(true);
81+
page.on("request", (request) => {
82+
const reqUrl = request.url();
83+
// http or https
84+
if (isValidUrl(reqUrl)) {
85+
const parsedUrl = new URL(reqUrl);
86+
parsedUrl.host = host;
87+
parsedUrl.protocol = "http:";
88+
parsedUrl.port = `${port}`;
89+
const parsedUrlString = parsedUrl.toString();
90+
request.continue({
91+
url: parsedUrlString,
92+
headers: Object.assign(
93+
{},
94+
request.headers(),
95+
{
96+
refer: parsedUrlString,
97+
// Same origin
98+
// origin: parsedUrl.origin,
99+
// CORS
100+
// host: parsedUrl.host,
101+
}),
102+
});
103+
}
104+
else {
105+
request.continue();
106+
}
107+
});
108+
}
109+
110+
await printer.render(location);
67111
const data = await printer.pdf(location, {
68112
format: "A4",
69113
...pdfOptions,
@@ -74,7 +118,7 @@ export const generatePdf = async ({
74118
}
75119

76120
singleBar.stop();
77-
await printer.close();
121+
await printer.closeBrowser();
78122

79123
const exportedPath = await mergePDF(normalizePages, outFile, outDir, pdfOutlines);
80124
const message = `\nExported to ${pc.yellow(exportedPath)}\n`;

packages/vuepress-plugin-export-pdf-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from "./types";
22
export * from "./runner";
33
export * from "./utils";
44
export * from "./configs";
5+
export * from "./generatePdf";
56
export * from "./systemInfo";

packages/vuepress-plugin-export-pdf-core/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface CommandOptions {
33
outDir?: string
44
outFile?: string
55
debug?: boolean
6+
urlOrigin?: string
67
pdfOutlines?: boolean
78
}
89

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from "./timeTransformer";
2-
export * from "./generatePdf";
32
export * from "./filterRoute";
43
export * from "./checkEnv";
54
export * from "./mergePDF";

packages/vuepress-plugin-export-pdf-v2/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The package provides the `press-export-pdf` command with the following command l
5454
- `--outFile <outFile>`: Name of output file
5555
- `--outDir <outDir>`: Directory of output files
5656
- `--pdfOutlines <pdfOutlines>`: Keep PDF outlines/bookmarks
57+
- `--urlOrigin <urlOrigin>`: Change the origin of the print url
5758
- `--debug`: Enable debug mode
5859
- `info`: Display environment information
5960
- `--help`: Display help information
@@ -111,7 +112,7 @@ config options:
111112
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
112113
- `pdfOptions` - [Valid options to configure PDF generation via Page.pdf()](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.pdfoptions.md) (default `{ format: 'A4 }`)
113114
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)
114-
115+
- `urlOrigin`: Change the origin of the print url([How do I change the URL point to the localhost](https://github.com/condorheroblog/vuepress-plugin/issues/5))
115116
## PDF print style
116117

117118
By default, `A4` paper is used for printing, The size of A4 paper is (8.27in x 11.7in), One inch is equal to ninety-six pixels: `1 in = 96 pixel (X)` ,the inch unit of A4 is converted to (793.92px x 1123.2px).

packages/vuepress-plugin-export-pdf-v2/assets/vuepress-plugin-export-pdf-v2-outline.tldr

+1-1
Large diffs are not rendered by default.

packages/vuepress-plugin-export-pdf-v2/assets/vuepress-plugin-export-pdf-v2.svg

+2-2
Loading

packages/vuepress-plugin-export-pdf-v2/example/vuepress-next/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"help": "press-export-pdf --help",
99
"version": "press-export-pdf --version",
1010
"systemInfo": "press-export-pdf info",
11+
"docs:urlOrigin": "press-export-pdf export docs --urlOrigin https://www.google.com/",
1112
"docs:outFile": "press-export-pdf export docs --outFile docs.pdf",
1213
"docs:outDir": "press-export-pdf export docs --outDir /tmp/outDir",
1314
"docs:debug": "press-export-pdf export docs --debug",

packages/vuepress-plugin-export-pdf-v2/src/commands/press-export-pdf.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const registerCommands = (program: CAC) => {
1515
.option("--outFile <outFile>", "Name of output file")
1616
.option("--outDir <outDir>", "Directory of output files")
1717
.option("--pdfOutlines <pdfOutlines>", "Keep PDF outlines/bookmarks")
18+
.option("--urlOrigin <urlOrigin>", "Change the origin of the print url")
1819
.option("--debug", "Enable debug mode")
1920
.action(serverApp);
2021

packages/vuepress-plugin-export-pdf-v2/src/serverApp.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
4545
sorter,
4646
puppeteerLaunchOptions,
4747
pdfOptions,
48-
pdfOutlines = commandOptions.pdfOutlines,
48+
routePatterns,
4949
outFile = vuepressOutFile,
5050
outDir = vuepressOutDir,
51-
routePatterns,
51+
urlOrigin = commandOptions.urlOrigin,
52+
pdfOutlines = commandOptions.pdfOutlines,
5253
} = userConfig;
5354

5455
const devApp = createDevApp({
@@ -79,6 +80,7 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
7980
outFile,
8081
outDir,
8182
sorter,
83+
urlOrigin,
8284
pdfOptions,
8385
pdfOutlines,
8486
routePatterns,

packages/vuepress-plugin-export-pdf-v2/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export interface UserConfig {
1616
outFile?: string
1717
outDir?: string
1818
pdfOutlines?: boolean
19+
urlOrigin?: string
1920
}

packages/vuepress-plugin-export-pdf/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The package provides the `press-export-pdf` command with the following command l
5353
- `-c, --config <config>`: Set path to config file
5454
- `--outFile <outFile>`: Name of output file
5555
- `--pdfOutlines <pdfOutlines>`: Keep PDF outlines/bookmarks
56+
- `--urlOrigin <urlOrigin>`: Change the origin of the print url
5657
- `--outDir <outDir>`: Directory of output files
5758
- `--theme <theme>`: Set VuePress theme
5859
- `--debug`: Enable debug mode
@@ -131,6 +132,7 @@ config options:
131132
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
132133
- `pdfOptions` - [Valid options to configure PDF generation via Page.pdf()](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.pdfoptions.md) (default `{ format: 'A4 }`)
133134
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)
135+
- `urlOrigin`: Change the origin of the print url([How do I change the URL point to the localhost](https://github.com/condorheroblog/vuepress-plugin/issues/5))
134136
## PDF print style
135137

136138
By default, `A4` paper is used for printing, The size of A4 paper is (8.27in x 11.7in), One inch is equal to ninety-six pixels: `1 in = 96 pixel (X)` ,the inch unit of A4 is converted to (793.92px x 1123.2px).

packages/vuepress-plugin-export-pdf/assets/vuepress-plugin-export-pdf-outline.tldr

+1-1
Large diffs are not rendered by default.

packages/vuepress-plugin-export-pdf/assets/vuepress-plugin-export-pdf.svg

+2-2
Loading

packages/vuepress-plugin-export-pdf/src/commands/press-export-pdf.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const registerCommands = (program: CAC, extendCliConfig?: UserConfig) =>
1717
.option("--theme <theme>", "Set VuePress theme")
1818
.option("--outFile <outFile>", "Name of output file")
1919
.option("--outDir <outDir>", "Directory of output files")
20+
.option("--urlOrigin <urlOrigin>", "Change the origin of the print url")
2021
.option("--debug", "Enable debug mode")
2122
.action((file: string, config: Record<string, string>) => {
2223
serverApp(file, extendCliConfig ?? config);

packages/vuepress-plugin-export-pdf/src/serverApp.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
5050
sorter,
5151
puppeteerLaunchOptions,
5252
pdfOptions,
53-
outFile = vuepressOutFile,
54-
outDir = vuepressOutDir,
55-
theme = vuepressTheme,
5653
routePatterns,
54+
theme = vuepressTheme,
55+
outDir = vuepressOutDir,
56+
outFile = vuepressOutFile,
57+
urlOrigin = commandOptions.urlOrigin,
5758
pdfOutlines = commandOptions.pdfOutlines,
5859
} = userConfig;
5960

@@ -87,6 +88,7 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
8788
outFile,
8889
outDir,
8990
sorter,
91+
urlOrigin,
9092
pdfOptions,
9193
pdfOutlines,
9294
routePatterns,

packages/vuepress-plugin-export-pdf/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export interface UserConfig {
1414
outFile?: string
1515
outDir?: string
1616
pdfOutlines?: boolean
17+
urlOrigin?: string
1718
}

0 commit comments

Comments
 (0)