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

Prdp 100 feat add playwright engine #37

Merged
merged 4 commits into from
Aug 24, 2023
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
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
Loading