Skip to content

Commit

Permalink
feat: support change the origin of the print url #5
Browse files Browse the repository at this point in the history
  • Loading branch information
condorheroblog committed Apr 29, 2023
1 parent 6d7c769 commit 76156f4
Show file tree
Hide file tree
Showing 20 changed files with 4,308 additions and 4,292 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ dist
**/.vuepress/.temp/
**/.vuepress/.cache/
**/.vuepress/dist/
*.pdf
2 changes: 1 addition & 1 deletion packages/vuepress-plugin-export-pdf-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"cac": "^6.7.14",
"envinfo": "^7.8.1",
"fs-extra": "^11.1.1",
"html-export-pdf-cli": "^1.0.2",
"html-export-pdf-cli": "^1.0.3",
"multimatch": "^6.0.0",
"ora": "^6.3.0",
"pdfjs": "^2.4.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { join } from "node:path";
import fse from "fs-extra";
import pc from "picocolors";
import { Printer, createProgress, writeFileSafe } from "html-export-pdf-cli";
import { Printer, createProgress, isValidUrl, writeFileSafe } from "html-export-pdf-cli";
import type { LaunchOptions, PDFOptions } from "html-export-pdf-cli";

import { filterRoute } from "../utils";
import type { Page } from "../";
import { mergePDF } from "./mergePDF";
import { filterRoute } from "./utils";
import type { Page } from ".";
import { mergePDF } from ".";

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

Expand All @@ -22,6 +22,7 @@ export interface IGeneratePdfOptions {
puppeteerLaunchOptions?: LaunchOptions
pdfOptions?: PDFOptions
pdfOutlines?: boolean
urlOrigin?: string
}

/**
Expand All @@ -36,10 +37,12 @@ export const generatePdf = async ({
sorter,
outFile,
outDir,
urlOrigin,
pdfOptions,
pdfOutlines,
routePatterns,
puppeteerLaunchOptions,
// eslint-disable-next-line sonarjs/cognitive-complexity
}: IGeneratePdfOptions) => {
const tempPdfDir = join(tempDir, "pdf");
fse.ensureDirSync(tempPdfDir);
Expand All @@ -49,21 +52,62 @@ export const generatePdf = async ({
if (typeof sorter === "function")
exportPages = exportPages.sort(sorter);

const isValidUrlOrigin = isValidUrl(urlOrigin ?? "");
if (urlOrigin && !isValidUrlOrigin) {
process.stdout.write(pc.red(`${urlOrigin} is not a valid URL`));
process.exit(1);
}

const normalizePages = exportPages.map((page) => {
return {
url: page.path,
title: page.title,
location: `http://${host}:${port}${page.path}`,
location: urlOrigin ? `${new URL(urlOrigin).origin}${page.path}` : `http://${host}:${port}${page.path}`,
pagePath: `${tempPdfDir}/${page.key}.pdf`,
};
});

const singleBar = createProgress();
singleBar.start(normalizePages.length);

const printer = new Printer(puppeteerLaunchOptions);
const printer = new Printer();
await printer.setup(puppeteerLaunchOptions);

for (const { location, pagePath, title } of normalizePages) {
const page = await printer.createNewPage(location);

if (urlOrigin && isValidUrlOrigin) {
await page.setRequestInterception(true);
page.on("request", (request) => {
const reqUrl = request.url();
// http or https
if (isValidUrl(reqUrl)) {
const parsedUrl = new URL(reqUrl);
parsedUrl.host = host;
parsedUrl.protocol = "http:";
parsedUrl.port = `${port}`;
const parsedUrlString = parsedUrl.toString();
request.continue({
url: parsedUrlString,
headers: Object.assign(
{},
request.headers(),
{
refer: parsedUrlString,
// Same origin
// origin: parsedUrl.origin,
// CORS
// host: parsedUrl.host,
}),
});
}
else {
request.continue();
}
});
}

await printer.render(location);
const data = await printer.pdf(location, {
format: "A4",
...pdfOptions,
Expand All @@ -74,7 +118,7 @@ export const generatePdf = async ({
}

singleBar.stop();
await printer.close();
await printer.closeBrowser();

const exportedPath = await mergePDF(normalizePages, outFile, outDir, pdfOutlines);
const message = `\nExported to ${pc.yellow(exportedPath)}\n`;
Expand Down
1 change: 1 addition & 0 deletions packages/vuepress-plugin-export-pdf-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./types";
export * from "./runner";
export * from "./utils";
export * from "./configs";
export * from "./generatePdf";
export * from "./systemInfo";
1 change: 1 addition & 0 deletions packages/vuepress-plugin-export-pdf-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface CommandOptions {
outDir?: string
outFile?: string
debug?: boolean
urlOrigin?: string
pdfOutlines?: boolean
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./timeTransformer";
export * from "./generatePdf";
export * from "./filterRoute";
export * from "./checkEnv";
export * from "./mergePDF";
3 changes: 2 additions & 1 deletion packages/vuepress-plugin-export-pdf-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The package provides the `press-export-pdf` command with the following command l
- `--outFile <outFile>`: Name of output file
- `--outDir <outDir>`: Directory of output files
- `--pdfOutlines <pdfOutlines>`: Keep PDF outlines/bookmarks
- `--urlOrigin <urlOrigin>`: Change the origin of the print url
- `--debug`: Enable debug mode
- `info`: Display environment information
- `--help`: Display help information
Expand Down Expand Up @@ -111,7 +112,7 @@ config options:
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
- `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 }`)
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)

- `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))
## PDF print style

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).
Expand Down

Large diffs are not rendered by default.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"help": "press-export-pdf --help",
"version": "press-export-pdf --version",
"systemInfo": "press-export-pdf info",
"docs:urlOrigin": "press-export-pdf export docs --urlOrigin https://www.google.com/",
"docs:outFile": "press-export-pdf export docs --outFile docs.pdf",
"docs:outDir": "press-export-pdf export docs --outDir /tmp/outDir",
"docs:debug": "press-export-pdf export docs --debug",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const registerCommands = (program: CAC) => {
.option("--outFile <outFile>", "Name of output file")
.option("--outDir <outDir>", "Directory of output files")
.option("--pdfOutlines <pdfOutlines>", "Keep PDF outlines/bookmarks")
.option("--urlOrigin <urlOrigin>", "Change the origin of the print url")
.option("--debug", "Enable debug mode")
.action(serverApp);

Expand Down
6 changes: 4 additions & 2 deletions packages/vuepress-plugin-export-pdf-v2/src/serverApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
sorter,
puppeteerLaunchOptions,
pdfOptions,
pdfOutlines = commandOptions.pdfOutlines,
routePatterns,
outFile = vuepressOutFile,
outDir = vuepressOutDir,
routePatterns,
urlOrigin = commandOptions.urlOrigin,
pdfOutlines = commandOptions.pdfOutlines,
} = userConfig;

const devApp = createDevApp({
Expand Down Expand Up @@ -79,6 +80,7 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
outFile,
outDir,
sorter,
urlOrigin,
pdfOptions,
pdfOutlines,
routePatterns,
Expand Down
1 change: 1 addition & 0 deletions packages/vuepress-plugin-export-pdf-v2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface UserConfig {
outFile?: string
outDir?: string
pdfOutlines?: boolean
urlOrigin?: string
}
2 changes: 2 additions & 0 deletions packages/vuepress-plugin-export-pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The package provides the `press-export-pdf` command with the following command l
- `-c, --config <config>`: Set path to config file
- `--outFile <outFile>`: Name of output file
- `--pdfOutlines <pdfOutlines>`: Keep PDF outlines/bookmarks
- `--urlOrigin <urlOrigin>`: Change the origin of the print url
- `--outDir <outDir>`: Directory of output files
- `--theme <theme>`: Set VuePress theme
- `--debug`: Enable debug mode
Expand Down Expand Up @@ -131,6 +132,7 @@ config options:
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
- `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 }`)
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)
- `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))
## PDF print style

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).
Expand Down

Large diffs are not rendered by default.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const registerCommands = (program: CAC, extendCliConfig?: UserConfig) =>
.option("--theme <theme>", "Set VuePress theme")
.option("--outFile <outFile>", "Name of output file")
.option("--outDir <outDir>", "Directory of output files")
.option("--urlOrigin <urlOrigin>", "Change the origin of the print url")
.option("--debug", "Enable debug mode")
.action((file: string, config: Record<string, string>) => {
serverApp(file, extendCliConfig ?? config);
Expand Down
8 changes: 5 additions & 3 deletions packages/vuepress-plugin-export-pdf/src/serverApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
sorter,
puppeteerLaunchOptions,
pdfOptions,
outFile = vuepressOutFile,
outDir = vuepressOutDir,
theme = vuepressTheme,
routePatterns,
theme = vuepressTheme,
outDir = vuepressOutDir,
outFile = vuepressOutFile,
urlOrigin = commandOptions.urlOrigin,
pdfOutlines = commandOptions.pdfOutlines,
} = userConfig;

Expand Down Expand Up @@ -87,6 +88,7 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
outFile,
outDir,
sorter,
urlOrigin,
pdfOptions,
pdfOutlines,
routePatterns,
Expand Down
1 change: 1 addition & 0 deletions packages/vuepress-plugin-export-pdf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface UserConfig {
outFile?: string
outDir?: string
pdfOutlines?: boolean
urlOrigin?: string
}

0 comments on commit 76156f4

Please sign in to comment.