Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: Support PDF rendering #487

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions proto/rendererv2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ message RenderRequest {
string timezone = 9;
map<string, StringList> headers = 10;
string authToken = 11;
string encoding = 12;
}

message RenderResponse {
Expand Down
24 changes: 21 additions & 3 deletions src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { Logger } from '../logger';
import { RenderingConfig } from '../config/rendering';
import { ImageRenderOptions, RenderOptions } from '../types';
import { getPDFOptionsFromURL } from './pdf';

export interface Metrics {
durationHistogram: promClient.Histogram;
Expand Down Expand Up @@ -319,6 +320,7 @@
}
}

const isPDF = options.encoding === 'pdf';
try {
await this.withTimingMetrics(() => {
if (this.config.verboseLogging) {
Expand Down Expand Up @@ -357,15 +359,15 @@
{
timeout: options.timeout * 1000,
},
options.fullPageImage || false
options.fullPageImage || isPDF
);
}, 'panelsRendered');
} catch (err) {
this.log.error('Error while waiting for the panels to load', 'url', options.url, 'err', err.stack);
}

if (!options.filePath) {
options.filePath = uniqueFilename(os.tmpdir()) + '.png';
options.filePath = uniqueFilename(os.tmpdir()) + (isPDF ? '.pdf' : '.png');
}

await this.setPageZoomLevel(page, this.config.pageZoomLevel);
Expand All @@ -381,10 +383,26 @@
height: scrollResult.scrollHeight,
});
}

if (isPDF) {
const scale = parseFloat((options.deviceScaleFactor as string) || '1') || 1;
return page.pdf({
...getPDFOptionsFromURL(options.url),
margin: {
bottom: 0,
top: 0,
right: 0,
left: 0,
},
path: options.filePath,
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
scale: 1/scale,
})
}

return page.screenshot({ path: options.filePath, fullPage: options.fullPageImage, captureBeyondViewport: options.fullPageImage || false });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we wrap this in if(!isPDF ) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary given the if(isPDF) ... case returns, but perhaps an else clause would be cleaner (I don't have any strong opinions here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I missed the return. na early return is better than if/else :)

}, 'screenshot');

if (options.scaleImage) {
if (options.scaleImage && !isPDF) {
const scaled = `${options.filePath}_${Date.now()}_scaled.png`;
const w = +options.width / options.scaleImage;
const h = +options.height / options.scaleImage;
Expand Down
13 changes: 13 additions & 0 deletions src/browser/pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as puppeteer from 'puppeteer';

// Allow setting the more esoteric PDF options via URL parameters
export function getPDFOptionsFromURL(url: string): puppeteer.PDFOptions {
const urlParams = new URLSearchParams(url);
return {
landscape: urlParams.get('pdf.landscape') !== 'false', // defaults true
format: (urlParams.get('pdf.format') as puppeteer.PaperFormat) ?? 'A4',
omitBackground: urlParams.get('pdf.omitBackground') === 'true', // defaults false,
printBackground: urlParams.get('pdf.printBackground') !== 'false', // defaults true,
pageRanges: urlParams.get('pdf.pageRanges') ?? undefined,
};
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 change: 1 addition & 0 deletions src/plugin/v2/grpc_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class PluginGRPCServer {
timezone: req.timezone,
deviceScaleFactor: req.deviceScaleFactor,
headers: headers,
encoding: req.encoding,
};

this.log.debug('Render request received', 'url', options.url);
Expand Down
1 change: 1 addition & 0 deletions src/plugin/v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface RenderRequest {
[header: string]: StringList;
};
authToken: string;
encoding: string;
}

export interface RenderResponse {
Expand Down