-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pr/577' into open-dev-v1
- Loading branch information
Showing
6 changed files
with
220 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
openhtmltopdf-objects/src/main/java/com/openhtmltopdf/objects/pdf/ForegroundPdfDrawer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.openhtmltopdf.objects.pdf; | ||
|
||
import com.openhtmltopdf.extend.OutputDevice; | ||
import com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice; | ||
import com.openhtmltopdf.render.RenderingContext; | ||
import org.apache.pdfbox.cos.COSArray; | ||
import org.apache.pdfbox.cos.COSName; | ||
import org.apache.pdfbox.cos.COSStream; | ||
import org.apache.pdfbox.multipdf.LayerUtility; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; | ||
import org.apache.pdfbox.util.Charsets; | ||
import org.w3c.dom.Element; | ||
|
||
import java.awt.*; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Map; | ||
|
||
public class ForegroundPdfDrawer extends PdfDrawerBase | ||
{ | ||
@Override | ||
public Map<Shape, String> drawObject(Element e, double x, double y, double width, double height, | ||
OutputDevice outputDevice, RenderingContext ctx, int dotsPerPixel) | ||
{ | ||
|
||
/* | ||
* We can only do something if this is a PDF. | ||
*/ | ||
if (!(outputDevice instanceof PdfBoxOutputDevice)) | ||
return null; | ||
|
||
PdfBoxOutputDevice pdfBoxOutputDevice = (PdfBoxOutputDevice) outputDevice; | ||
|
||
try | ||
{ | ||
LayerUtility layerUtility = new LayerUtility(pdfBoxOutputDevice.getWriter()); | ||
PDFormXObject pdFormXObject = importPageAsXForm(ctx, e, pdfBoxOutputDevice, | ||
layerUtility); | ||
PDPage page = pdfBoxOutputDevice.getPage(); | ||
|
||
/* | ||
* This ensures that the Contents of the page is a COSArray. The first entry in | ||
* the array is just a save state (e.g. 'q'), the last one is just a restore 'Q'. | ||
* We can override that to add the XForm. | ||
*/ | ||
layerUtility.wrapInSaveRestore(page); | ||
COSArray cosArray = (COSArray) page.getCOSObject() | ||
.getDictionaryObject(COSName.CONTENTS); | ||
|
||
COSStream restoreStateAndPlaceWatermark = (COSStream) cosArray.get(cosArray.size() - 1); | ||
OutputStream watermarkOutputStream = restoreStateAndPlaceWatermark.createOutputStream(); | ||
watermarkOutputStream.write("Q\nq\n".getBytes(Charsets.US_ASCII)); | ||
COSName name = page.getResources().add(pdFormXObject); | ||
name.writePDF(watermarkOutputStream); | ||
watermarkOutputStream.write(' '); | ||
watermarkOutputStream.write("Do\n".getBytes(Charsets.US_ASCII)); | ||
watermarkOutputStream.write("Q\n".getBytes(Charsets.US_ASCII)); | ||
watermarkOutputStream.close(); | ||
} | ||
catch (IOException e1) | ||
{ | ||
e1.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
openhtmltopdf-objects/src/main/java/com/openhtmltopdf/objects/pdf/PdfDrawerBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
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<PDFBoxDeviceReference, SoftReference<Map<String, PDFormXObject>>> formMap = new HashMap<PDFBoxDeviceReference, SoftReference<Map<String, PDFormXObject>>>(); | ||
|
||
protected PDFormXObject importPageAsXForm(RenderingContext ctx, Element e, | ||
PdfBoxOutputDevice pdfBoxOutputDevice, LayerUtility layerUtility) throws IOException | ||
{ | ||
|
||
Map<String, PDFormXObject> 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<String, PDFormXObject> getFormCacheMap(PdfBoxOutputDevice pdfBoxOutputDevice) | ||
{ | ||
SoftReference<Map<String, PDFormXObject>> mapWeakReference = formMap | ||
.get(new PDFBoxDeviceReference(pdfBoxOutputDevice)); | ||
Map<String, PDFormXObject> map = null; | ||
if (mapWeakReference != null) | ||
map = mapWeakReference.get(); | ||
if (map == null) | ||
{ | ||
map = new HashMap<String, PDFormXObject>(); | ||
formMap.put(new PDFBoxDeviceReference(pdfBoxOutputDevice), | ||
new SoftReference<Map<String, PDFormXObject>>(map)); | ||
} | ||
return map; | ||
} | ||
|
||
protected int getPageNumber(Element e) | ||
{ | ||
String pdfpageValue = e.getAttribute("pdfpage"); | ||
if (pdfpageValue == null || pdfpageValue.isEmpty()) | ||
pdfpageValue = "1"; | ||
return Integer.parseInt(pdfpageValue); | ||
} | ||
|
||
private static class PDFBoxDeviceReference extends WeakReference<PdfBoxOutputDevice> | ||
{ | ||
PDFBoxDeviceReference(PdfBoxOutputDevice referent) | ||
{ | ||
super(referent); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) | ||
{ | ||
if (obj instanceof PDFBoxDeviceReference) | ||
{ | ||
return ((PDFBoxDeviceReference) obj).get() == get(); | ||
} | ||
return super.equals(obj); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
PdfBoxOutputDevice pdfBoxOutputDevice = get(); | ||
if (pdfBoxOutputDevice != null) | ||
return pdfBoxOutputDevice.hashCode(); | ||
return 0; | ||
} | ||
} | ||
} |