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

adding RTL support #303

Open
wants to merge 1 commit into
base: main
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
20 changes: 19 additions & 1 deletion pdf-workitem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
</organization>

<dependencies>
<dependency>
<!-- ALWAYS required, usually included transitively. -->
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<!-- Required for PDF output. -->
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<!-- Optional, leave out if you do not need right-to-left or bi-directional text support. -->
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-rtl-support</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
Expand Down Expand Up @@ -202,4 +220,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.jbpm.document.Document;
import org.jbpm.document.service.impl.DocumentImpl;
import org.jbpm.process.workitem.core.AbstractLogOrThrowWorkItemHandler;
Expand All @@ -40,7 +36,12 @@
import org.kie.api.runtime.process.WorkItemManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.openhtmltopdf.bidi.support.ICUBidiReorderer;
import com.openhtmltopdf.bidi.support.ICUBidiSplitter;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;



@Wid(widfile = "GeneratePDFDefinitions.wid", name = "GeneratePDF",
displayName = "GeneratePDF",
Expand Down Expand Up @@ -72,8 +73,8 @@ public class GeneratePDFWorkitemHandler extends AbstractLogOrThrowWorkItemHandle
public void executeWorkItem(WorkItem workItem,
WorkItemManager workItemManager) {

try {

try {
RequiredParameterValidator.validate(this.getClass(),
workItem);

Expand All @@ -85,40 +86,32 @@ public void executeWorkItem(WorkItem workItem,
pdfName = "generatedpdf";
}

Configuration cfg = new Configuration(freemarker.template.Configuration.VERSION_2_3_26);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);

StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("pdfTemplate",
templateXHTML);
cfg.setTemplateLoader(stringLoader);
logger.info(templateXHTML);

StringWriter stringWriter = new StringWriter();

Template pdfTemplate = cfg.getTemplate("pdfTemplate");
pdfTemplate.process(workItem.getParameters(),
stringWriter);
resultXHTML = stringWriter.toString();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(resultXHTML);
renderer.layout();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFastMode();
builder.withHtmlContent(templateXHTML, null);
builder.toStream(baos);

// Enable RTL support
builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
builder.defaultTextDirection(PdfRendererBuilder.TextDirection.RTL);

builder.run();

Document document = new DocumentImpl();
document.setName(pdfName + ".pdf");
document.setLastModified(new Date());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
document.setContent(baos.toByteArray());

results.put(RESULTS_VALUE,
document);
results.put(RESULTS_VALUE, document);

workItemManager.completeWorkItem(workItem.getId(),
results);
workItemManager.completeWorkItem(workItem.getId(), results);
} catch (Exception e) {
logger.error(e.getMessage());
handleException(e);
Expand All @@ -128,9 +121,5 @@ public void executeWorkItem(WorkItem workItem,
public void abortWorkItem(WorkItem workItem,
WorkItemManager manager) {
}

// for testing
public String getResultXHTML() {
return resultXHTML;
}

}