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

Minor renames in PDF export code #4116

Merged
merged 2 commits into from
Aug 25, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/lesmis-server/src/routes/PDFExportRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { Request, Response, NextFunction } from 'express';
import { Route } from '@tupaia/server-boilerplate';
import { downloadPageAsPdf } from '@tupaia/tsutils';
import { downloadPageAsPDF } from '@tupaia/tsutils';

type Body = {
pdfPageUrl: string;
Expand All @@ -28,7 +28,7 @@ export class PDFExportRoute extends Route<PDFExportRequest> {
const { pdfPageUrl } = this.req.body;
const { cookie, host: cookieDomain } = this.req.headers;

const buffer = await downloadPageAsPdf(pdfPageUrl, cookie, cookieDomain);
const buffer = await downloadPageAsPDF(pdfPageUrl, cookie, cookieDomain);
this.res.set({
'Content-Type': 'application/pdf',
'Content-Length': buffer.length,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cookie from 'cookie';
import puppeteer from 'puppeteer';

const verifyPdfPageUrl = (pdfPageUrl: string): string => {
const verifyPDFPageUrl = (pdfPageUrl: string): string => {
const lesmisValidDomains = ['lesmis.la', 'www.lesmis.la'];
if (!pdfPageUrl || typeof pdfPageUrl !== 'string') {
throw new Error(`'pdfPageUrl' should be provided in request body, got: ${pdfPageUrl}`);
Expand All @@ -19,16 +19,16 @@ const verifyPdfPageUrl = (pdfPageUrl: string): string => {

const buildParams = (pdfPageUrl: string, userCookie: string, cookieDomain: string | undefined) => {
const cookies = cookie.parse(userCookie || '');
const verifiedPdfPageUrl = verifyPdfPageUrl(pdfPageUrl);
const location = new URL(verifiedPdfPageUrl);
const verifiedPDFPageUrl = verifyPDFPageUrl(pdfPageUrl);
const location = new URL(verifiedPDFPageUrl);
const finalisedCookieObjects = Object.keys(cookies).map(name => ({
name,
domain: cookieDomain,
url: location.origin,
httpOnly: true,
value: cookies[name],
}));
return { verifiedPdfPageUrl, cookies: finalisedCookieObjects };
return { verifiedPDFPageUrl, cookies: finalisedCookieObjects };
};

/**
Expand All @@ -37,20 +37,20 @@ const buildParams = (pdfPageUrl: string, userCookie: string, cookieDomain: strin
* @param cookieDomain the domain of cookie, required when setting up cookie in page
* @returns pdf buffer
*/
export const downloadPageAsPdf = async (
export const downloadPageAsPDF = async (
pdfPageUrl: string,
userCookie = '',
cookieDomain: string | undefined,
) => {
let browser;
let buffer;
const { cookies, verifiedPdfPageUrl } = buildParams(pdfPageUrl, userCookie, cookieDomain);
const { cookies, verifiedPDFPageUrl } = buildParams(pdfPageUrl, userCookie, cookieDomain);

try {
browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setCookie(...cookies);
await page.goto(verifiedPdfPageUrl, { timeout: 60000, waitUntil: 'networkidle0' });
await page.goto(verifiedPDFPageUrl, { timeout: 60000, waitUntil: 'networkidle0' });
buffer = await page.pdf({
format: 'a4',
printBackground: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/tsutils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './validation';
export * from './types';
export * from './PDFExporter';
export * from './downloadPageAsPDF';
4 changes: 2 additions & 2 deletions packages/web-config-server/src/export/PDFExportHandler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { downloadPageAsPdf } from '@tupaia/tsutils';
import { downloadPageAsPDF } from '@tupaia/tsutils';
import { convertToCDNHost } from '@tupaia/utils';

export const PDFExportHandler = async (req, res) => {
const { pdfPageUrl } = req.body;
const { cookie, host, via } = req.headers;
const cookieDomain = via && via.includes('cloudfront.net') ? convertToCDNHost(host) : host;

const buffer = await downloadPageAsPdf(pdfPageUrl, cookie, cookieDomain);
const buffer = await downloadPageAsPDF(pdfPageUrl, cookie, cookieDomain);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': buffer.length,
Expand Down