Skip to content

Commit dc500db

Browse files
committed
Fix svg scaling for certain books [#5]
1 parent e5b9681 commit dc500db

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

src/item/BiBoxBook.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { delay, promisePool } from '../util/promise';
1+
import { promisePool } from '../util/promise';
22
import { Book } from './Book';
33
import { defDownloadOptions, DownloadOptions } from './download-options';
4+
import { getPdfOptions } from './get-pdf-options';
45

56
export class BiBoxBook extends Book {
67
async download(outDir: string, _options?: DownloadOptions) {
@@ -63,15 +64,7 @@ export class BiBoxBook extends Book {
6364
const pdfFile = this.getPdfPath(dir, pageNo);
6465

6566
await page.pdf({
66-
height: await page.evaluate(
67-
() =>
68-
window.document.querySelector('img')?.height ||
69-
window.innerHeight
70-
),
71-
width: await page.evaluate(
72-
() =>
73-
window.document.querySelector('img')?.width || window.innerWidth
74-
),
67+
...(await getPdfOptions(page, options)),
7568
path: pdfFile,
7669
});
7770
} finally {

src/item/DigiBook.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@ import { waitForGoto } from '../util/puppeteer';
33
import { URL } from 'url';
44
import { Book } from './Book';
55
import { defDownloadOptions, DownloadOptions } from './download-options';
6+
import { SizeAttributes, getPdfOptions } from './get-pdf-options';
67

78
export class DigiBook extends Book {
89
async download(outDir: string, _options?: DownloadOptions) {
910
const dir = await this.mkSubDir(outDir);
1011
const options = defDownloadOptions(_options);
12+
options.format ??= 'a4';
1113

1214
// Get url of 1st svg page
1315
const checkPage = await this.shelf.browser.newPage();
1416
let page1Url: string;
17+
let sizeHint: SizeAttributes;
1518
try {
1619
await checkPage.goto(new URL(`?page=1`, this.url).toString(), {
1720
waitUntil: 'networkidle2',
1821
timeout: this.shelf.options.timeout,
1922
});
20-
page1Url = await checkPage.$eval(
23+
[page1Url, sizeHint] = await checkPage.$eval(
2124
'#pg1 > object',
22-
(obj) => (obj as HTMLObjectElement).data
25+
(obj: HTMLObjectElement): [string, SizeAttributes] => [
26+
obj.data,
27+
{ width: obj.width, height: obj.height },
28+
]
2329
);
2430
} finally {
2531
await checkPage.close();
@@ -54,7 +60,7 @@ export class DigiBook extends Book {
5460
const pdfFile = this.getPdfPath(dir, pageNo);
5561

5662
await page.pdf({
57-
format: options.format,
63+
...(await getPdfOptions(page, options, sizeHint)),
5864
path: pdfFile,
5965
});
6066

src/item/ScookBook.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ScrapeError } from '../error/ScrapeError';
22
import { delay, promisePool } from '../util/promise';
33
import { Book } from './Book';
44
import { defDownloadOptions, DownloadOptions } from './download-options';
5+
import { getPdfOptions } from './get-pdf-options';
56

67
export class ScookBook extends Book {
78
async download(outDir: string, _options?: DownloadOptions) {
@@ -84,15 +85,7 @@ export class ScookBook extends Book {
8485
const pdfFile = this.getPdfPath(dir, pageNo);
8586

8687
await page.pdf({
87-
height: await page.evaluate(
88-
() =>
89-
window.document.querySelector('img')?.height ||
90-
window.innerHeight
91-
),
92-
width: await page.evaluate(
93-
() =>
94-
window.document.querySelector('img')?.width || window.innerWidth
95-
),
88+
...(await getPdfOptions(page, options)),
9689
path: pdfFile,
9790
});
9891
} finally {

src/item/download-options.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ export interface DownloadOptions {
77
format?: PaperFormat;
88
}
99

10-
const defaultOptions: Required<DownloadOptions> = {
10+
const defaultOptions: DownloadOptions = {
1111
concurrency: 10,
1212
mergePdfs: true,
13-
format: 'a4',
1413
};
1514

1615
export function defDownloadOptions(_options?: DownloadOptions) {

src/item/get-pdf-options.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { PDFOptions, Page } from 'puppeteer';
2+
import { DownloadOptions } from './download-options';
3+
4+
export interface SizeAttributes {
5+
width: string;
6+
height: string;
7+
}
8+
9+
export async function getPdfOptions(
10+
page: Page,
11+
options: DownloadOptions,
12+
sizeHint?: SizeAttributes
13+
): Promise<PDFOptions> {
14+
const size = await page.evaluate(async (sizeHint): Promise<PDFOptions> => {
15+
const root = document.documentElement;
16+
17+
if (root instanceof SVGGraphicsElement) {
18+
if (sizeHint) {
19+
root.setAttribute('width', sizeHint.width);
20+
root.setAttribute('height', sizeHint.height);
21+
return sizeHint;
22+
}
23+
const bbox = root.getBBox();
24+
return { width: bbox.width, height: bbox.height };
25+
}
26+
27+
if (sizeHint) return sizeHint;
28+
29+
return {
30+
width:
31+
document.querySelector<HTMLImageElement>('body > img')?.width ??
32+
window.innerWidth,
33+
height:
34+
document.querySelector<HTMLImageElement>('body > img')?.height ??
35+
window.innerHeight,
36+
};
37+
}, sizeHint);
38+
39+
return {
40+
format: options.format,
41+
printBackground: true,
42+
...size,
43+
};
44+
}

0 commit comments

Comments
 (0)