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

[VAS-812] feat: updates to pdf-engine #99

Merged
merged 4 commits into from
Mar 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"axios": "^1.5.0",
"express": "^4.18.2",
"fs-extra": "^11.1.1",
"handlebars": "^4.7.7",
"handlebars-helpers": "^0.10.0",
"hbs-cli": "^1.4.1",
"handlebars": "^4.7.8",
"puppeteer": "^22.4.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"puppeteer": "^18.0.2"
"nodemon": "^3.0.1"
},
"devDependencies": {
"@types/jest": "^24.0.15",
Expand Down
1 change: 1 addition & 0 deletions node/pdf-generate/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("generatePdf", () => {

it("should return a pdf when passing valid data", async () => {
const formData = new FormData();
formData.append('title', 'Test Title');
formData.append('data', '{\n' +
'\t\t"transaction": {\n' +
'\t\t\t"id": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789",\n' +
Expand Down
8 changes: 7 additions & 1 deletion node/pdf-generate/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const generatePdf = async function (req, res, next) {
page = await browser.newPage();

let data = req.body.data;
let title = req.body.title;

if (title == undefined) {
title = "Documento PDF PagoPA";
}

if (data == undefined) {
res.status(400);
Expand Down Expand Up @@ -99,6 +104,7 @@ const generatePdf = async function (req, res, next) {
await waitForRender(page);
await page.pdf({
path: path.join(workingDir, "pagopa-receipt.pdf"),
title: title,
format: 'A4',
landscape: false,
printBackground: true,
Expand Down Expand Up @@ -153,7 +159,7 @@ const waitForRender = async (page, timeout = 30000) => {
}

lastSize = currentSize;
await page.waitForTimeout(checkInterval);
await new Promise(r => setTimeout(r, checkInterval))
}
};

Expand Down
2 changes: 1 addition & 1 deletion node/pdf-generate/helpers/eq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var eq = function (a, b, options) {
return a === b ? options.fn(this) : null;
return a === b ? options.fn(this) : options.inverse(this);
};

module.exports = eq;
1 change: 0 additions & 1 deletion node/pdf-generate/utils/browserManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const puppeteer = require('puppeteer');
const registerHelpers = require("handlebars-helpers");
let handlebars = require("handlebars");
let splitAndSpace = require('../helpers/splitAndSpace');
let not = require('../helpers/not')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum AppErrorCodeEnum {

PDFE_714("PDFE_708", "I/O error when reading generate type from request, and writing it to the output stream"),

PDFE_715("PDFE_715", "I/O error when reading title from request and writing it to the output stream"),

PDFE_896("PDFE_896", "Unexpected field in request body"),
PDFE_897("PDFE_897", "Invalid HTML template, template not provided"),
PDFE_898("PDFE_898", "Invalid document data"),
Expand All @@ -41,8 +43,6 @@ public enum AppErrorCodeEnum {
PDFE_907("PDFE_907", "I/O error on handling the PDF generation result"),
PDFE_908("PDFE_908", "I/O error on creating the working directory");



private final String errorCode;
private final String errorMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GeneratePDFInput {
private boolean applySignature;
private boolean generateZipped;
private ZipFile templateZip;
private String title;

private GeneratorType generatorType = GeneratorType.ITEXT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class PdfEngineRequest {

String data;

String workingDirPath;
ZipFile template;
String workingDirPath;

String title;

ZipFile template;


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public BufferedInputStream generatePDF(GeneratePDFInput generatePDFInput, Path w

PdfEngineClientImpl pdfEngineClient = PdfEngineClientImpl.getInstance();
PdfEngineRequest pdfEngineRequest = new PdfEngineRequest();
pdfEngineRequest.setTitle(generatePDFInput.getTitle() != null ?
generatePDFInput.getTitle() : "Documento PDF PagoPA");
pdfEngineRequest.setWorkingDirPath(workingDirPath.toFile().getAbsolutePath());
pdfEngineRequest.setData(ObjectMapperUtils.writeValueAsString(generatePDFInput.getData()));
pdfEngineRequest.setTemplate(generatePDFInput.getTemplateZip());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public GeneratePDFInput retrieveInputData(byte[] requestBody, Map<String, String
case "data":
generatePDFInput.setData(getDocumentInputData(multipartStream));
break;
case "title":
generatePDFInput.setTitle(getStringField(multipartStream, PDFE_715));
break;
case "applySignature":
generatePDFInput.setApplySignature(getBooleanField(multipartStream, PDFE_708));
break;
Expand Down Expand Up @@ -87,6 +90,17 @@ private boolean getBooleanField(MultipartStream multipartStream, AppErrorCodeEnu
return Boolean.parseBoolean(outputStream.toString());
}

private String getStringField(MultipartStream multipartStream, AppErrorCodeEnum errorCode) throws RequestBodyParseException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
multipartStream.readBodyData(outputStream);
} catch (IOException e) {
throw new RequestBodyParseException(errorCode, errorCode.getErrorMessage(), e);
}
return outputStream.toString();
}



private Map<String,Object> getDocumentInputData(MultipartStream multipartStream) throws RequestBodyParseException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void generatePDFZippedWithSuccess() {
pdfInput.setData(Collections.singletonMap("a", "b"));
pdfInput.setApplySignature(false);
pdfInput.setGenerateZipped(true);
pdfInput.setTitle("title");

PdfEngineResponse pdfEngineResponse = new PdfEngineResponse();
pdfEngineResponse.setStatusCode(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ void retrieveInputDataSuccess() {
doReturn(
String.format(HEADER_TEMPLATE, "applySignature"),
String.format(HEADER_TEMPLATE, "generateZipped"),
String.format(HEADER_TEMPLATE, "title"),
String.format(HEADER_TEMPLATE, "data")
).when(multipartStreamMock).readHeaders();
doReturn(Collections.singletonMap("ke1", "value1")).when(objectMapperMock).readValue(anyString(), any(TypeReference.class));
doReturn(true, true, false).when(multipartStreamMock).readBoundary();
doReturn(true, true, true, false).when(multipartStreamMock).readBoundary();

GeneratePDFInput result = sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath);

Expand Down Expand Up @@ -306,4 +307,23 @@ void retrieveInputDataFailReadBoundaryThrowsMalformedStreamException() {

Assertions.assertEquals(AppErrorCodeEnum.PDFE_710, e.getErrorCode());
}

@Test
@SneakyThrows
void retrieveInputDataFailReadTitleThrowsIOException() {
MultipartStream multipartStreamMock = mock(MultipartStream.class);

doReturn(multipartStreamMock).when(sut).getMultipartStream(any(), anyString());
doReturn(true).when(multipartStreamMock).skipPreamble();
doReturn(String.format(HEADER_TEMPLATE, "title")).when(multipartStreamMock).readHeaders();
doThrow(IOException.class).when(multipartStreamMock).readBodyData(any());

RequestBodyParseException e = assertThrows(
RequestBodyParseException.class,
() -> sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath)
);

Assertions.assertEquals(AppErrorCodeEnum.PDFE_715, e.getErrorCode());
}

}
Loading