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

POC for bruk av Gotenberg til å generere PDF av søknad #1380

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .nais/fyllut/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ spec:
value: {{skjemabygging-proxy-url}}
- name: SKJEMABYGGING_PROXY_CLIENT_ID
value: {{skjemabygging-proxy-client-id}}
- name: GOTENBERG_URL
value: {{gotenberg-url}}
- name: SEND_INN_TOKEN_X_CLIENT_ID
value: {{send-inn-token-x-client-id}}
- name: SEND_INN_HOST
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/dev-delingslenke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
forms-api-url: https://forms-api.nav.no
Expand Down Expand Up @@ -46,6 +47,9 @@ accessPolicy:
- application: nav-dekoratoren
namespace: personbruker
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
nils-arne marked this conversation as resolved.
Show resolved Hide resolved
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -40,6 +41,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/preprod-alt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
nils-arne marked this conversation as resolved.
Show resolved Hide resolved
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -47,6 +48,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -48,6 +49,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 031863f2-9881-44af-ace5-6b5ab52afeb1
skjemabygging-proxy-url: https://skjemabygging-proxy.prod-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: prod-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -45,6 +46,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: prod-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: prod-gcp
external:
- host: skjemabygging-proxy.prod-fss-pub.nais.io
- host: formio-api.intern.nav.no
2 changes: 1 addition & 1 deletion mocks/mocks/collections.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"get-translations:success",
"post-azure-openid-token:success",
"get-kodeverk-currencies:success",
"post-exstream-pdf:success",
"post-gotenberg-pdf:success",
"get-tokenx-endpoint:success",
"post-tokenx:success",
"post-soknad:success",
Expand Down
4 changes: 2 additions & 2 deletions mocks/mocks/routes/skjemabygging-proxy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = [
{
id: 'post-exstream-pdf',
url: '/skjemabygging-proxy/exstream',
id: 'post-gotenberg-pdf',
url: '/skjemabygging-proxy/extream',
method: 'POST',
variants: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/fyllut-backend/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ TOKEN_X_WELL_KNOWN_URL=http://127.0.0.1:3300/tokenx/.well-known
AMPLITUDE_API_ENDPOINT=http://127.0.0.1:3300/amplitude/collect-auto
AMPLITUDE_DISABLE_BATCH=true
# FYLLUT_BACKEND_LOGLEVEL=debug
GOTENBERG_URL=http://127.0.0.1:3000
3 changes: 3 additions & 0 deletions packages/fyllut-backend/icons/nav-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 2 additions & 7 deletions packages/fyllut-backend/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@

const innsendingsId = '65ed0008-ec72-4c90-8b44-165d3c265da0';
const sendInnLocation = 'http://www.unittest.nav.no/sendInn/123';
const azureTokenEndpoint = process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT!;
const tokenxEndpoint = 'http://tokenx-unittest.nav.no/token';
const applicationData = {
form: {
Expand All @@ -249,11 +248,8 @@
innsendingsId,
};

const azureOpenidScope = nock(extractHost(azureTokenEndpoint))
.post(extractPath(azureTokenEndpoint))
.reply(200, { access_token: 'azure-access-token' });
const skjemabyggingproxyScope = nock(process.env.SKJEMABYGGING_PROXY_URL as string)
.post('/exstream')
const skjemabyggingproxyScope = nock(process.env.GOTENBERG_URL as string)
.post('/gotenberg')
.reply(200, { data: { result: [{ content: { data: '' } }] } });
const tokenxWellKnownScope = nock(extractHost(tokenxConfig?.wellKnownUrl))
.get(extractPath(tokenxConfig?.wellKnownUrl))
Expand All @@ -270,10 +266,9 @@
.send(applicationData)
.set('Fyllut-Submission-Method', 'digital')
.set('Authorization', `Bearer ${createMockIdportenJwt({ pid: '12345678911' }, undefined, key)}`); // <-- injected by idporten sidecar
expect(res.status).toBe(201);

Check failure on line 269 in packages/fyllut-backend/src/app.test.ts

View workflow job for this annotation

GitHub Actions / test-coverage

src/app.test.ts > app > Performs TokenX exchange and retrieves pdf from exstream before calling SendInn

AssertionError: expected 500 to be 201 // Object.is equality - Expected + Received - 201 + 500 ❯ src/app.test.ts:269:24
expect(res.headers['location']).toMatch(sendInnLocation);

azureOpenidScope.done();
skjemabyggingproxyScope.done();
tokenxWellKnownScope.done();
tokenEndpointNockScope.done();
Expand Down
4 changes: 3 additions & 1 deletion packages/fyllut-backend/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const localDevelopmentConfig: DefaultConfig = {
decoratorUrl: 'https://www.nav.no/dekoratoren?simple=true',
skjemabyggingProxyUrl: process.env.SKJEMABYGGING_PROXY_URL || 'https://skjemabygging-proxy.dev-fss-pub.nais.io',
skjemabyggingProxyClientId: '95170319-b4d7-4190-8271-118ed19bafbf',
gotenbergUrl: process.env.GOTENBERG_URL || 'https://convert-to-pdf.intern.dev.nav.no',
azureOpenidTokenEndpoint:
process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT ||
'https://login.microsoftonline.com/966ac572-f5b7-4bbe-aa88-c76419c0f851/oauth2/v2.0/token',
Expand Down Expand Up @@ -125,6 +126,7 @@ const defaultConfig: DefaultConfig = {
decoratorUrl: process.env.DECORATOR_URL!,
skjemabyggingProxyUrl: process.env.SKJEMABYGGING_PROXY_URL!,
skjemabyggingProxyClientId: process.env.SKJEMABYGGING_PROXY_CLIENT_ID!,
gotenbergUrl: process.env.GOTENBERG_URL!,
azureOpenidTokenEndpoint: process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT!,
clientId: process.env.AZURE_APP_CLIENT_ID!,
skjemaDir: process.env.SKJEMA_DIR!,
Expand Down Expand Up @@ -171,4 +173,4 @@ const checkConfigConsistency = (config: ConfigType, logError = logger.error, exi
}
};

export { checkConfigConsistency, config };
export { checkConfigConsistency, config, defaultConfig };
1 change: 1 addition & 0 deletions packages/fyllut-backend/src/config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type DefaultConfig = {
decoratorUrl: string;
skjemabyggingProxyUrl: string;
skjemabyggingProxyClientId: string;
gotenbergUrl: string;
azureOpenidTokenEndpoint: string;
clientId: string;
skjemaDir?: string;
Expand Down
45 changes: 0 additions & 45 deletions packages/fyllut-backend/src/routers/api/exstream.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Submission,
} from '@navikt/skjemadigitalisering-shared-domain';
import { NextFunction, Request, Response } from 'express';
import { base64Decode } from '../../utils/base64';
import { htmlResponseError } from '../../utils/errorHandling';
import { logErrorWithStacktrace } from '../../utils/errors';
import { createPdf } from './helpers/pdfService';
Expand All @@ -28,7 +27,7 @@ const parseBody = (
return { form, submission, submissionMethod, translations, language };
};

const exstream = {
const gotenberg = {
post: async (req: Request, res: Response, next: NextFunction) => {
try {
const { form, submission, submissionMethod, translations, language } = parseBody(req);
Expand All @@ -42,8 +41,9 @@ const exstream = {
translations,
languageCode,
);
res.contentType(pdf.contentType);
res.send(base64Decode(pdf.data));

res.setHeader('Content-Type', 'application/pdf');
res.status(200).send(Buffer.from(new Uint8Array(pdf)));
} catch (e) {
logErrorWithStacktrace(e as Error);
const createPdfError = htmlResponseError('Generering av PDF feilet');
Expand All @@ -52,4 +52,4 @@ const exstream = {
},
};

export default exstream;
export default gotenberg;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.footer {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 90%;
margin-left: 64px;
margin-right: 64px;
border-top: 1px solid #ccc;
}
.line1 {
display: flex;
justify-content: space-between;
margin-top:6px;
}
.line2 {
display: flex;
justify-content: space-between;
}

body {
font-family: "Arial";
font-size: 10px;
margin: auto 24px;
}

</style>
</head>
<body>
<div class="footer">
<div class="line1">
<span>{{userIdLabel}}: {{USER_ID}}</span>
<span>{{createdLabel}}: {{CREATION_DATE}}</span>
</div>

<div class="line2">
<span>{{schemaNumberLabel}}: {{SCHEMA_NUMBER}}</span>
<span>{{versionLabel}}: {{SCHEMA_VERSION}}</span>
<div><span>{{pageLabel}} </span><span class="pageNumber"></span><span> {{ofLabel}} </span><span class="totalPages"></span></div>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { generateFooterHtml } from './footerBuilder';

const mockTranslate = (text: string) => text;

describe('footerBuilder', () => {
describe('createFooter', () => {
const userId: string = '12345678901';
const schemaVersion: string = '342u43590534mklgfd0i9';
const skjemanummer: string = 'NAV 11-12.24B';
const language: string = 'nb';

let footer: string;

it('creates a footer html document with labels and the supplied data', () => {
footer = generateFooterHtml(userId, schemaVersion, skjemanummer, language, mockTranslate);
expect(footer).toContain(userId);
expect(footer).toContain(schemaVersion);
expect(footer).toContain(skjemanummer);
expect(footer).toContain('<span>F.nr: ' + userId + '</span>');
expect(footer).toContain('<span>Opprettet: ');
expect(footer).toContain('<span>Skjemanummer: ' + skjemanummer + '</span>');
expect(footer).toContain('<span>Versjon: ' + schemaVersion + '</span>');
expect(footer).toContain(
'<span>Side </span><span class="pageNumber"></span><span> av </span><span class="totalPages"></span>',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TEXTS } from '@navikt/skjemadigitalisering-shared-domain';
import { readFileSync } from 'fs';
import path from 'path';

const filePath = path.join(process.cwd(), '/src/routers/api/helpers/footer-template.html');

export function generateFooterHtml(userId, schemaVersion, skjemanummer, language, translate: (text: string) => string) {
let footerHtml = readFileSync(filePath, { encoding: 'utf-8', flag: 'r' });

const creationDate = new Date().toLocaleString();

footerHtml = footerHtml
.replace('{{USER_ID}}', userId)
.replace('{{CREATION_DATE}}', creationDate)
.replace('{{SCHEMA_VERSION}}', schemaVersion)
.replace('{{SCHEMA_NUMBER}}', skjemanummer)
.replace('{{userIdLabel}}', translate(TEXTS.statiske.footer.userIdLabel))
.replace('{{createdLabel}}', translate(TEXTS.statiske.footer.createdDatelabel))
.replace('{{schemaNumberLabel}}', translate(TEXTS.statiske.footer.schemaNumberLabel))
.replace('{{versionLabel}}', translate(TEXTS.statiske.footer.versionLabel))
.replace('{{pageLabel}}', translate(TEXTS.statiske.footer.pageLabel))
.replace('{{ofLabel}}', translate(TEXTS.statiske.footer.ofLabel));

return footerHtml;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,21 @@ p {margin: 0}
.alt {margin-bottom: 5px; font-family: "Courier New", sans-serif; font-style: italic;}
.innrykk {margin: 0px 0px 10px 20px;}
.underskrift {margin-bottom: 30px;}
.navlogored {height: 16px;float: left;margin: 4px auto 2px auto;}
@page {size: a4 portrait;margin-bottom:56px;}
section {page-break-after: always; break-after: page;}
</style>`;

const body = (formSummaryObject: SummaryPanel[], confirmation?: string, signatures?: string) => `
<body>
${navlogo}
${formSummaryObject.map(section).join('')}
${confirmation || ''}
${signatures || ''}
</body>`;

const navlogo: string = `<img xmlns="http://www.w3.org/2000/svg " src="nav-logo.svg" alt="NAV logo" class="navlogored" x="0" height="16px"/>`;

const section = (formSection: SummaryPanel) => `
<h2>${formSection.label}</h2>
${sectionContent(formSection.components, 1)}`;
Expand Down
Loading
Loading