com.openhtmltopdf.objects.pdf.PdfDrawerBase Maven / Gradle / Ivy
package com.openhtmltopdf.objects.pdf;
import com.openhtmltopdf.extend.FSObjectDrawer;
import com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice;
import com.openhtmltopdf.render.RenderingContext;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.w3c.dom.Element;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public abstract class PdfDrawerBase implements FSObjectDrawer
{
private final Map>> formMap = new HashMap>>();
protected PDFormXObject importPageAsXForm(RenderingContext ctx, Element e,
PdfBoxOutputDevice pdfBoxOutputDevice, LayerUtility layerUtility) throws IOException
{
Map map = getFormCacheMap(pdfBoxOutputDevice);
int pdfpage = getPageNumber(e);
String pdfsrc = e.getAttribute("pdfsrc");
String url = ctx.getUac().resolveURI(pdfsrc);
PDFormXObject pdFormXObject = map.get(url);
if (pdFormXObject == null)
{
try (InputStream inputStream = new URL(url).openStream())
{
PDFParser pdfParser = new PDFParser(new RandomAccessBuffer(inputStream));
pdfParser.parse();
pdFormXObject = layerUtility
.importPageAsForm(pdfParser.getPDDocument(), pdfpage - 1);
pdfParser.getPDDocument().close();
}
map.put(url, pdFormXObject);
}
return pdFormXObject;
}
protected Map getFormCacheMap(PdfBoxOutputDevice pdfBoxOutputDevice)
{
SoftReference