Skip to content

Commit

Permalink
feat: REST API for getting prepared HTML content for WeasyPrint
Browse files Browse the repository at this point in the history
Refs: #116
  • Loading branch information
grigoriev committed Aug 1, 2024
1 parent d8b770f commit e0fa11f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,47 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
PdfGenerationLog generationLog = new PdfGenerationLog();
generationLog.log("Starting html generation");

@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final LiveDocHelper.DocumentData documentData = getDocumentData(exportParams, project);
@NotNull String htmlContent = prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);

generationLog.log("Html is ready, starting pdf generation");
if (PdfExporterExtensionConfiguration.getInstance().isDebug()) {
new HtmlLogger().log(documentData.getDocumentContent(), htmlContent, generationLog.getLog());
}
byte[] bytes = generatePdf(documentData, exportParams, metaInfoCallback, htmlContent, generationLog);

if (exportParams.getInternalContent() == null) { //do not log time for internal parts processing
String finalMessage = "PDF document '" + documentData.getDocumentTitle() + "' has been generated within " + (System.currentTimeMillis() - startTime) + " milliseconds";
logger.info(finalMessage);
generationLog.log(finalMessage);
}
return bytes;
}

public @NotNull String prepareHtmlContentForWeasyPrint(@NotNull ExportParams exportParams, @Nullable ExportMetaInfoCallback metaInfoCallback) {
@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final LiveDocHelper.DocumentData documentData = getDocumentData(exportParams, project);
return prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);
}

private @Nullable ITrackerProject getTrackerProject(@NotNull ExportParams exportParams) {
ITrackerProject project = null;
if (!StringUtils.isEmpty(exportParams.getProjectId())) {
project = pdfExporterPolarionService.getTrackerProject(exportParams.getProjectId());
}
return project;
}

final LiveDocHelper.DocumentData documentData =
switch (exportParams.getDocumentType()) {
case WIKI -> liveDocHelper.getWikiDocument(project, exportParams);
case REPORT -> liveDocHelper.getLiveReport(project, exportParams);
case DOCUMENT -> liveDocHelper.getLiveDocument(Objects.requireNonNull(project), exportParams, true);
};
private @NotNull LiveDocHelper.DocumentData getDocumentData(@NotNull ExportParams exportParams, @Nullable ITrackerProject project) {
return switch (exportParams.getDocumentType()) {
case WIKI -> liveDocHelper.getWikiDocument(project, exportParams);
case REPORT -> liveDocHelper.getLiveReport(project, exportParams);
case DOCUMENT -> liveDocHelper.getLiveDocument(Objects.requireNonNull(project), exportParams, true);
};
}

private @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ITrackerProject project, @NotNull LiveDocHelper.DocumentData documentData, @Nullable ExportMetaInfoCallback metaInfoCallback) {
String cssContent = getCssContent(documentData, exportParams);
String preparedDocumentContent = postProcessDocumentContent(exportParams, project, documentData.getDocumentContent());
String headerFooterContent = getHeaderFooterContent(documentData, exportParams);
Expand All @@ -97,20 +126,7 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
if (metaInfoCallback != null) {
metaInfoCallback.setLinkedWorkItems(WorkItemRefData.extractListFromHtml(htmlContent, exportParams.getProjectId()));
}
htmlContent = htmlProcessor.internalizeLinks(htmlContent);

generationLog.log("Html is ready, starting pdf generation");
if (PdfExporterExtensionConfiguration.getInstance().isDebug()) {
new HtmlLogger().log(documentData.getDocumentContent(), htmlContent, generationLog.getLog());
}
byte[] bytes = generatePdf(documentData, exportParams, metaInfoCallback, htmlContent, generationLog);

if (exportParams.getInternalContent() == null) { //do not log time for internal parts processing
String finalMessage = "PDF document '" + documentData.getDocumentTitle() + "' has been generated within " + (System.currentTimeMillis() - startTime) + " milliseconds";
logger.info(finalMessage);
generationLog.log(finalMessage);
}
return bytes;
return htmlProcessor.internalizeLinks(htmlContent);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public Response convertToPdf(ExportParams exportParams) {
return polarionService.callPrivileged(() -> super.convertToPdf(exportParams));
}

@Override
public String prepareHtmlContentForWeasyPrint(ExportParams exportParams) {
return polarionService.callPrivileged(() -> super.prepareHtmlContentForWeasyPrint(exportParams));
}

@Override
public Response startPdfConverterJob(ExportParams exportParams) {
// In async case logout inside the filter must be deactivated. Async Job itself will care about logout after finishing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ public Response convertToPdf(ExportParams exportParams) {
return Response.ok(pdfBytes).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=document.pdf").build();
}

@POST
@Path("/prepared-html-content")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@Tag(name = "PDF Processing")
@Operation(summary = "Returns prepared HTML which will be used for PDF conversion using WeasyPrint",
responses = {
@ApiResponse(responseCode = "200",
description = "Prepared HTML content",
content = {@Content(mediaType = MediaType.TEXT_HTML)}
)
})
public String prepareHtmlContentForWeasyPrint(ExportParams exportParams) {
validateExportParameters(exportParams);
return pdfConverter.prepareHtmlContentForWeasyPrint(exportParams, null);
}

@POST
@Path("/convert/jobs")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit e0fa11f

Please sign in to comment.