mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
## Description Currently certificate translations on production sometimes does not show the required language. This could not be replicated when creating certificates on staging (Browserless.io) and local development (Chromium), which means this fix ultimately cannot be tested unless on live. This is an attempt to fix it by isolating the certificate generation into it's own context, and applying a cookie to define the required language. This fix is based on the assumption that there is some sort of error which pushes the certificate to be generated on the client side, which ultimately will render in English due to constraints on nextjs. ## Changes Made - Apply language into cookie instead purely dynamically on SSR - Minor unrelated fixes ## Testing Performed Tested to ensure certificates could still be generated
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { DateTime } from 'luxon';
|
|
import type { Browser } from 'playwright';
|
|
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
import type { SupportedLanguageCodes } from '../../constants/i18n';
|
|
import { encryptSecondaryData } from '../crypto/encrypt';
|
|
|
|
export type GetCertificatePdfOptions = {
|
|
documentId: number;
|
|
language?: SupportedLanguageCodes;
|
|
};
|
|
|
|
export const getCertificatePdf = async ({ documentId, language }: GetCertificatePdfOptions) => {
|
|
const { chromium } = await import('playwright');
|
|
|
|
const encryptedId = encryptSecondaryData({
|
|
data: documentId.toString(),
|
|
expiresAt: DateTime.now().plus({ minutes: 5 }).toJSDate().valueOf(),
|
|
});
|
|
|
|
let browser: Browser;
|
|
|
|
if (process.env.NEXT_PRIVATE_BROWSERLESS_URL) {
|
|
// !: Use CDP rather than the default `connect` method to avoid coupling to the playwright version.
|
|
// !: Previously we would have to keep the playwright version in sync with the browserless version to avoid errors.
|
|
browser = await chromium.connectOverCDP(process.env.NEXT_PRIVATE_BROWSERLESS_URL);
|
|
} else {
|
|
browser = await chromium.launch();
|
|
}
|
|
|
|
if (!browser) {
|
|
throw new Error(
|
|
'Failed to establish a browser, please ensure you have either a Browserless.io url or chromium browser installed',
|
|
);
|
|
}
|
|
|
|
const browserContext = await browser.newContext();
|
|
|
|
const page = await browserContext.newPage();
|
|
|
|
if (language) {
|
|
await page.context().addCookies([
|
|
{
|
|
name: 'language',
|
|
value: language,
|
|
url: NEXT_PUBLIC_WEBAPP_URL(),
|
|
},
|
|
]);
|
|
}
|
|
|
|
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, {
|
|
waitUntil: 'networkidle',
|
|
timeout: 10_000,
|
|
});
|
|
|
|
const result = await page.pdf({
|
|
format: 'A4',
|
|
});
|
|
|
|
await browserContext.close();
|
|
|
|
void browser.close();
|
|
|
|
return result;
|
|
};
|