Skip to content

Commit

Permalink
Prdp 100 feat add playwright engine (#37)
Browse files Browse the repository at this point in the history
* [PRDP-100] feat: Changed structure to intialize Playwright chromium

* [PRDP-100] feat: Changed structure to intialize Playwright chromium

* Bump to version 1.6.2-PRDP-100-feat-add-playwright-engine [skip ci]

---------

Co-authored-by: acialini <[email protected]>
Co-authored-by: pagopa-github-bot <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2023
1 parent 4d78216 commit b415582
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ file and other optional attachments, such as CSS files, that will be used to gen
> The HTML template file must be named to match the value of the `HTML_TEMPLATE_FILE_NAME` environment variable
> (default is `template`) (example of HTML template file name: `template.html`)
### Playwright version

In order to use the newly introduced engine you may use the parameter

```
...
--form 'generatorType="PLAYWRIGHT"'
```

The two values available are PLAYWRIGHT and ITEXT (the value defaults to ITEXT currently)

---

## Develop Locally 💻
Expand Down
3 changes: 3 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@
},
"generateZipped": {
"type": "boolean"
},
"generatorType": {
"type": "string"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion performance-test/src/modules/pdf_engine_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export function generatePDF(pdfEngineUri, subKey, zipFile, inputData, generateZi
const form = {
data: inputData,
template: http.file(zipFile, 'template.zip'),
generateZipped: generateZipped
generateZipped: generateZipped,
generatorType: "PLAYWRIGHT"
};

let headers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ public class HttpTriggerGeneratePDFFunction {
private Playwright playwright;

public HttpTriggerGeneratePDFFunction() throws GeneratePDFException {
playwright = Playwright.create();
BrowserType chromium = playwright.chromium();
this.generatePDFService = new GeneratePDFServiceImpl(buildHandlebars(),
chromium.launch(new BrowserType.LaunchOptions().setHeadless(true)).newContext());

this.generatePDFService = new GeneratePDFServiceImpl(buildHandlebars());
this.parseRequestBodyService = new ParseRequestBodyServiceImpl(new ObjectMapper());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ public class GeneratePDFServiceImpl implements GeneratePDFService {
private final String htmlTemplateFileName = System.getenv().getOrDefault("HTML_TEMPLATE_FILE_NAME", "template");

private final Handlebars handlebars;
private final BrowserContext context;

public GeneratePDFServiceImpl(Handlebars handlebars, BrowserContext browserContext) throws GeneratePDFException {
public GeneratePDFServiceImpl(Handlebars handlebars) throws GeneratePDFException {
this.handlebars = handlebars;
this.context = browserContext;
}

@Override
Expand Down Expand Up @@ -182,21 +179,28 @@ private String createUsingPlaywright(Path workingDirPath, File pdfTempFile, Stri
FileUtils.writeByteArrayToFile(new File(workingDirPath.toAbsolutePath()
+ UNZIPPED_FILES_FOLDER + "/filledTemplate.html"), filledTemplate.getBytes());

try (Page page = context.newPage()) {

page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.SCREEN));
page.navigate("file:" + workingDirPath.toAbsolutePath() + UNZIPPED_FILES_FOLDER + "/filledTemplate.html");
page.waitForLoadState(LoadState.NETWORKIDLE);
page.pdf(new Page.PdfOptions().setFormat("A4").setPath(pdfTempFile.getAbsoluteFile().toPath()));
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();

try (BrowserContext context = chromium.launch(new BrowserType.LaunchOptions().setHeadless(true)).newContext();
Page page = context.newPage()) {
page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.SCREEN));
page.navigate("file:" + workingDirPath.toAbsolutePath() + UNZIPPED_FILES_FOLDER + "/filledTemplate.html");
page.waitForLoadState(LoadState.NETWORKIDLE);
page.pdf(new Page.PdfOptions().setFormat("A4").setPath(pdfTempFile.getAbsoluteFile().toPath()));

//Create a PdfStandardsConverter instance, passing in the input file as a parameter
PdfStandardsConverter converter = new PdfStandardsConverter(pdfTempFile.getAbsolutePath());
//Create a PdfStandardsConverter instance, passing in the input file as a parameter
PdfStandardsConverter converter = new PdfStandardsConverter(pdfTempFile.getAbsolutePath());

//Convert to PdfA2A
converter.toPdfA2A(pdfTempFile.getParent() + "/ToPdfA2A.pdf");
//Convert to PdfA2A
converter.toPdfA2A(pdfTempFile.getParent() + "/ToPdfA2A.pdf");

return pdfTempFile.getParent() + "/ToPdfA2A.pdf";
}

return pdfTempFile.getParent() + "/ToPdfA2A.pdf";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void setUp() throws IOException, GeneratePDFException {
browserContextMock = mock(BrowserContext.class);
pageMock = mock(Page.class);

sut = spy(new GeneratePDFServiceImpl(handlebarsMock, browserContextMock));
sut = spy(new GeneratePDFServiceImpl(handlebarsMock));
workingPath = Files.createTempDirectory("testDir");
}

Expand Down

0 comments on commit b415582

Please sign in to comment.