Skip to content

Commit

Permalink
add support for external version of PD4ML
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jul 4, 2023
1 parent 0c5bc24 commit f08fc10
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 32 deletions.
4 changes: 2 additions & 2 deletions build.number
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Wed Jun 28 15:35:36 CEST 2023
build.number=21
#Tue Jul 04 15:21:25 CEST 2023
build.number=5
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bundlename: pdf.extension
filename: pdf-extension
bundleversion: 1.1.0.
bundleversion: 1.2.0.
id: 66E312DD-D083-27C0-64189D16753FD6F0
label: PDF Extension
luceeCoreVersion: 5.0.0.244-SNAPSHOT
Expand Down
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ start-bundles: false
<exclude name="**/jsp-*"/>
<exclude name="**/lucee.jar"/>
<exclude name="**/org.apache.felix.framework-*"/>
<exclude name="**/pd4ml*.jar"/>
</fileset>
</copy>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.net.URL;

import org.lucee.extension.pdf.PDFDocument;
import org.lucee.extension.pdf.pd4ml.lib.PDFBy;
import org.lucee.extension.pdf.pd4ml.lib.PDFByFactory;
import org.lucee.extension.pdf.util.ClassUtil;
import org.lucee.extension.pdf.util.XMLUtil;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -71,7 +73,7 @@ public PD4MLPDFDocument() {
@Override
public byte[] _render(Dimension dimension, double unitFactor, PageContext pc, boolean generateOutlines) throws PageException, IOException {
ConfigWeb config = pc.getConfig();
PDFByReflection pd4ml = new PDFByReflection(config);
PDFBy pd4ml = PDFByFactory.getInstance(config);
pd4ml.generateOutlines(generateOutlines);
pd4ml.enableTableBreaks(true);
pd4ml.interpolateImages(true);
Expand Down Expand Up @@ -111,7 +113,7 @@ public byte[] _render(Dimension dimension, double unitFactor, PageContext pc, bo
return baos.toByteArray();
}

private void content(PDFByReflection pd4ml, PageContext pc, OutputStream os) throws PageException, IOException {
private void content(PDFBy pd4ml, PageContext pc, OutputStream os) throws PageException, IOException {
ConfigWeb config = pc.getConfig();

if (fontDirectory != null) {
Expand All @@ -128,7 +130,8 @@ private void content(PDFByReflection pd4ml, PageContext pc, OutputStream os) thr
try {
body = beautifyHTML(pc, new InputSource(new StringReader(body)), base);
}
catch (Exception e) {}
catch (Exception e) {
}
pd4ml.render(body, os, base);

}
Expand Down Expand Up @@ -232,7 +235,7 @@ private void patchPD4MLProblems(PageContext pc, Document xml, URL base) {
toLocalSource(CFMLEngineFactory.getInstance(), pc, xml.getDocumentElement(), base.getHost() + ":" + base.getPort());
}

private void render(PageContext pc, PDFByReflection pd4ml, InputStream is, OutputStream os, URL base) throws IOException, PageException {
private void render(PageContext pc, PDFBy pd4ml, InputStream is, OutputStream os, URL base) throws IOException, PageException {
try {

// text/html
Expand Down
47 changes: 47 additions & 0 deletions source/java/src/org/lucee/extension/pdf/pd4ml/lib/PDFBy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.lucee.extension.pdf.pd4ml.lib;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import org.lucee.extension.pdf.PDFPageMark;

import lucee.runtime.exp.PageException;

public interface PDFBy {
public PDFBy newInstance() throws PageException;

public void enableTableBreaks(boolean b) throws PageException;

public void interpolateImages(boolean b) throws PageException;

public void adjustHtmlWidth() throws PageException;

public void setPageInsets(Insets insets) throws PageException;

public void setPageSize(Dimension dimension) throws PageException;

public void generateOutlines(boolean flag) throws PageException;

public void useTTF(String pathToFontDirs, boolean embed) throws PageException;

public boolean isPro() throws PageException;

public void overrideDocumentEncoding(String encoding) throws PageException;

public void setDefaultTTFs(String string, String string2, String string3) throws PageException;

public void render(InputStreamReader reader, OutputStream os) throws PageException;

public BufferedImage[] renderAsImages(URL url, int width, int height) throws PageException;

public void render(String str, OutputStream os, URL base) throws PageException;

public void setPageHeader(PDFPageMark header) throws PageException;

public void setPageFooter(PDFPageMark footer) throws PageException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.lucee.extension.pdf.pd4ml.lib;

import lucee.commons.io.log.Log;
import lucee.runtime.config.Config;
import lucee.runtime.exp.PageException;

public class PDFByFactory {
private static Object token = new Object();
private static PDFBy instance;

public static PDFBy getInstance(Config config) throws PageException {
if (instance == null) {
synchronized (token) {
if (instance == null) {
Log log = config.getLog("application");
try {
instance = new PDFByReflection(config);
log.info("PDF", "using PD4ML via reflection from system Classloader");
}
catch (NoClassDefFoundError err) {
log.error("PDF", err);
instance = new PDFByInnerReflection(config);
log.info("PDF", "using PD4ML via reflection from bundled version");
}

}
}
}
return instance.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package org.lucee.extension.pdf.pd4ml;
package org.lucee.extension.pdf.pd4ml.lib;

import java.awt.Dimension;
import java.awt.Insets;
Expand All @@ -42,17 +42,17 @@
import lucee.runtime.util.IO;
import lucee.runtime.util.ResourceUtil;

public class PDFByReflection {
class PDFByInnerReflection implements PDFBy {

private final Object pd4ml;
private Object pd4ml;
private CFMLEngine engine;
private Cast caster;
private static Class pd4mlClass;
private static ClassLoader classLoader;
private static Class pd4mlMarkClass;
// private final boolean isEvaluation;

public PDFByReflection(Config config) throws PageException {
public PDFByInnerReflection(Config config) throws PageException {
engine = CFMLEngineFactory.getInstance();
caster = engine.getCastUtil();
ClassUtil util = engine.getClassUtil();
Expand Down Expand Up @@ -122,72 +122,102 @@ public PDFByReflection(Config config) throws PageException {
classLoader = new URLClassLoader(urls, parent);
}
if (pd4mlClass == null) pd4mlClass = util.loadClass(classLoader, "org.zefer.pd4ml.PD4ML");
pd4ml = util.loadInstance(pd4mlClass);

}
catch (Exception e) {
throw caster.toPageException(e);
}
pd4mlClass = pd4ml.getClass();
}

private PDFByInnerReflection(CFMLEngine engine, Cast caster, Object instance) {
this.engine = engine;
this.caster = caster;
pd4mlClass = instance.getClass();
pd4ml = instance;
}

@Override
public PDFBy newInstance() throws PageException {
try {
return new PDFByInnerReflection(engine, caster, CFMLEngineFactory.getInstance().getClassUtil().loadInstance(pd4mlClass));
}
catch (Exception e) {
throw caster.toPageException(e);
}
}

@Override
public void enableTableBreaks(boolean b) throws PageException {
invoke(pd4ml, "enableTableBreaks", b);
}

@Override
public void interpolateImages(boolean b) throws PageException {
invoke(pd4ml, "interpolateImages", b);
}

@Override
public void adjustHtmlWidth() throws PageException {
invoke(pd4ml, "adjustHtmlWidth");
}

@Override
public void setPageInsets(Insets insets) throws PageException {
invoke(pd4ml, "setPageInsets", insets);
}

@Override
public void setPageSize(Dimension dimension) throws PageException {
invoke(pd4ml, "setPageSize", dimension);
}

@Override
public void setPageHeader(PDFPageMark header) throws PageException {
invoke(pd4ml, "setPageHeader", toPD4PageMark(header));
}

@Override
public void generateOutlines(boolean flag) throws PageException {
invoke(pd4ml, "generateOutlines", new Object[] { caster.toBoolean(flag) }, new Class[] { boolean.class });
}

@Override
public void useTTF(String pathToFontDirs, boolean embed) throws PageException {
invoke(pd4ml, "useTTF", new Object[] { pathToFontDirs, caster.toBoolean(embed) }, new Class[] { String.class, boolean.class });
}

@Override
public boolean isPro() throws PageException {
return CFMLEngineFactory.getInstance().getCastUtil().toBooleanValue(invoke(pd4ml, "isPro", new Object[] {}, new Class[] {}));
}

@Override
public void overrideDocumentEncoding(String encoding) throws PageException {
invoke(pd4ml, "overrideDocumentEncoding", new Object[] { encoding }, new Class[] { String.class });
}

@Override
public void setDefaultTTFs(String string, String string2, String string3) throws PageException {
invoke(pd4ml, "setDefaultTTFs", new Object[] { string, string2, string3 }, new Class[] { String.class, String.class, String.class });
}

@Override
public void setPageFooter(PDFPageMark footer) throws PageException {
// if(isEvaluation) return;
invoke(pd4ml, "setPageFooter", toPD4PageMark(footer));
}

@Override
public void render(InputStreamReader reader, OutputStream os) throws PageException {
invoke(pd4ml, "render", new Object[] { reader, os }, new Class[] { reader.getClass(), OutputStream.class });
}

@Override
public BufferedImage[] renderAsImages(URL url, int width, int height) throws PageException {
return (BufferedImage[]) invoke(pd4ml, "renderAsImages", new Object[] { url, width, height }, new Class[] { URL.class, int.class, int.class });
}

@Override
public void render(String str, OutputStream os, URL base) throws PageException {
// setEvaluationFooter();

Expand Down
Loading

0 comments on commit f08fc10

Please sign in to comment.