-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#344 - Embed PDF page via an img tag. (WIP)
Nearly there, the only reason I havent committed the test is that it is a couple of pixels off on width, which is noticeable when there is a border.
- Loading branch information
Showing
11 changed files
with
272 additions
and
10 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
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.
51 changes: 51 additions & 0 deletions
51
openhtmltopdf-examples/src/main/resources/visualtest/html/pdf-linked-from-img-tag.html
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,51 @@ | ||
<html> | ||
<head> | ||
<style> | ||
@page { | ||
|
||
} | ||
img { | ||
margin-top: 5px; | ||
display: block; | ||
box-sizing: border-box; | ||
border: 1px solid red; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<!-- Intrinsic size --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="" /> | ||
|
||
<!-- Width larger than real life --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 100%;" /> | ||
|
||
<!-- Width smaller than real life --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 3cm;" /> | ||
|
||
<!-- max-width --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 100%; max-width: 200px;" /> | ||
|
||
<!-- min-width --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 3cm;min-width: 70%;" /> | ||
|
||
<!-- content-box --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 50%; padding: 50px; box-sizing: content-box;" /> | ||
|
||
<!-- border-box --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 50%; padding: 50px; box-sizing: border-box;" /> | ||
|
||
<!-- width and height --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="width: 5cm; height: 5cm;" /> | ||
|
||
<!-- Height larger than real life --> | ||
<img src="../../demos/images/hello.pdf" alt="PDF" style="height: 20cm;" /> | ||
|
||
<!-- max-height and second page --> | ||
<img src="../../demos/images/hello.pdf" page="2" alt="PDF" style="height: 20cm; max-height: 3cm;" /> | ||
|
||
<!-- error handling --> | ||
<img src="non-existant-pdf.pdf" alt="PDF" style="" /> | ||
<img src="../../demos/images/hello.pdf" page="15" alt="PDF" style="" /> | ||
|
||
</body> | ||
</html> |
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
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
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
149 changes: 149 additions & 0 deletions
149
openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxPDFReplacedElement.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,149 @@ | ||
/* | ||
* {{{ header & license | ||
* Copyright (c) 2006 Wisconsin Court System | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public License | ||
* as published by the Free Software Foundation; either version 2.1 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
* }}} | ||
*/ | ||
package com.openhtmltopdf.pdfboxout; | ||
|
||
import com.openhtmltopdf.css.style.CssContext; | ||
import com.openhtmltopdf.layout.LayoutContext; | ||
import com.openhtmltopdf.layout.SharedContext; | ||
import com.openhtmltopdf.pdfboxout.PdfBoxLinkManager.IPdfBoxElementWithShapedLinks; | ||
import com.openhtmltopdf.render.BlockBox; | ||
import com.openhtmltopdf.render.Box; | ||
import com.openhtmltopdf.render.RenderingContext; | ||
import com.openhtmltopdf.swing.ImageMapParser; | ||
import com.openhtmltopdf.util.XRLog; | ||
|
||
import org.apache.pdfbox.multipdf.LayerUtility; | ||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException; | ||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; | ||
import org.w3c.dom.Element; | ||
|
||
import java.awt.Point; | ||
import java.awt.Rectangle; | ||
import java.awt.Shape; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
|
||
public class PdfBoxPDFReplacedElement implements PdfBoxReplacedElement, IPdfBoxElementWithShapedLinks { | ||
private final PDFormXObject _srcFormObject; | ||
private final float _width; | ||
private final float _height; | ||
private final Map<Shape, String> _imageMap; | ||
private Point _location = new Point(0, 0); | ||
|
||
private PdfBoxPDFReplacedElement(PDFormXObject srcForm, Element e, Box box, CssContext ctx, SharedContext shared, float w, float h) { | ||
this._srcFormObject = srcForm; | ||
this._width = w; | ||
this._height = h; | ||
this._imageMap = ImageMapParser.findAndParseMap(e, shared); | ||
} | ||
|
||
private static int parsePage(Element e) { | ||
if (e.getAttribute("page").isEmpty()) { | ||
return 0; | ||
} | ||
|
||
try { | ||
return Integer.parseInt(e.getAttribute("page")) - 1; | ||
} catch (NumberFormatException e1) { | ||
XRLog.exception("Unable to parse page of img tag with PDF!", e1); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public static PdfBoxPDFReplacedElement create(PDDocument target, byte[] pdfBytes, Element e, Box box, CssContext ctx, SharedContext shared) { | ||
try (PDDocument srcDocument = PDDocument.load(pdfBytes)){ | ||
int pageNo = parsePage(e); | ||
if (pageNo >= srcDocument.getNumberOfPages()) { | ||
XRLog.load(Level.WARNING, "Page does not exist for pdf in img tag. Ignoring!"); | ||
return null; | ||
} | ||
|
||
PDPage page = srcDocument.getPage(pageNo); | ||
float width = page.getMediaBox().getWidth() * shared.getDotsPerPixel(); | ||
float height = page.getMediaBox().getHeight() * shared.getDotsPerPixel(); | ||
|
||
LayerUtility util = new LayerUtility(target); | ||
PDFormXObject formXObject = util.importPageAsForm(srcDocument, page); | ||
|
||
return new PdfBoxPDFReplacedElement(formXObject, e, box, ctx, shared, width, height); | ||
} catch (InvalidPasswordException e1) { | ||
XRLog.exception("Tried to open a password protected document as src for an img!", e1); | ||
} catch (IOException e1) { | ||
XRLog.exception("Could not read pdf passed as src for img element!", e1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public int getIntrinsicWidth() { | ||
return (int) _width; | ||
} | ||
|
||
@Override | ||
public int getIntrinsicHeight() { | ||
return (int) _height; | ||
} | ||
|
||
@Override | ||
public Point getLocation() { | ||
return _location; | ||
} | ||
|
||
@Override | ||
public void setLocation(int x, int y) { | ||
_location = new Point(x, y); | ||
} | ||
|
||
@Override | ||
public Map<Shape, String> getLinkMap() { | ||
return _imageMap; | ||
} | ||
|
||
@Override | ||
public void detach(LayoutContext c) { | ||
} | ||
|
||
@Override | ||
public boolean isRequiresInteractivePaint() { | ||
// N/A | ||
return false; | ||
} | ||
|
||
@Override | ||
public void paint(RenderingContext c, PdfBoxOutputDevice outputDevice, BlockBox box) { | ||
Rectangle contentBounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c); | ||
outputDevice.drawPdfAsImage(_srcFormObject, contentBounds, getIntrinsicWidth(), getIntrinsicHeight()); | ||
} | ||
|
||
@Override | ||
public int getBaseline() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean hasBaseline() { | ||
return false; | ||
} | ||
} |
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
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
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