Skip to content

Commit

Permalink
Merge pull request #628 from rototor/use_pdfbox_image_bytes_encoder
Browse files Browse the repository at this point in the history
Use the PDFBox builtin image direct embedding
  • Loading branch information
danfickle authored Jan 2, 2021
2 parents 08665aa + 2eee07d commit 1290eba
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.RenderingHints.Key;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
Expand Down Expand Up @@ -794,15 +792,7 @@ public Stroke getStroke() {
public void realizeImage(PdfBoxImage img) {
PDImageXObject xobject;
try {
if (img.isJpeg()) {
xobject = JPEGFactory.createFromStream(_writer,
new ByteArrayInputStream(img.getBytes()));
} else {
BufferedImage buffered = ImageIO.read(new ByteArrayInputStream(
img.getBytes()));

xobject = LosslessFactory.createFromImage(_writer, buffered);
}
xobject = PDImageXObject.createFromByteArray(_writer, img.getBytes(), img.getUri());
} catch (IOException e) {
throw new PdfContentStreamAdapter.PdfException("realizeImage", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class PdfBoxImage implements FSImage {
private float _intrinsicWidth;
private float _intrinsicHeight;

private final boolean _isJpeg;

private PDImageXObject _xobject;

public PdfBoxImage(byte[] image, String uri) throws IOException {
Expand All @@ -40,12 +38,6 @@ public PdfBoxImage(byte[] image, String uri) throws IOException {
reader.setInput(in);
_intrinsicWidth = reader.getWidth(0);
_intrinsicHeight = reader.getHeight(0);

String type = reader.getFormatName();

_isJpeg = (type.equalsIgnoreCase("jpeg")
|| type.equalsIgnoreCase("jpg") || type
.equalsIgnoreCase("jfif"));
} else {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.LOAD_UNRECOGNIZED_IMAGE_FORMAT_FOR_URI, uri);
// TODO: Avoid throw here.
Expand All @@ -58,12 +50,11 @@ public PdfBoxImage(byte[] image, String uri) throws IOException {
}

public PdfBoxImage(byte[] bytes, String uri, float width, float height,
boolean isJpeg, PDImageXObject xobject) {
PDImageXObject xobject) {
this._bytes = bytes;
this._uri = uri;
this._intrinsicWidth = width;
this._intrinsicHeight = height;
this._isJpeg = isJpeg;
this._xobject = xobject;
}

Expand All @@ -77,7 +68,7 @@ public FSImage scaleToOutputResolution(float dotsPerPixel) {
height *= factor;
}

return new PdfBoxImage(_bytes, _uri, width, height, _isJpeg, _xobject);
return new PdfBoxImage(_bytes, _uri, width, height, _xobject);
}

@Override
Expand Down Expand Up @@ -141,8 +132,4 @@ public void setXObject(PDImageXObject xobject) {
public String getUri() {
return _uri;
}

public boolean isJpeg() {
return _isJpeg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,7 @@ public Stroke getStroke() {
public void realizeImage(PdfBoxImage img) {
PDImageXObject xobject;
try {
if (img.isJpeg()) {
xobject = JPEGFactory.createFromStream(_writer,
new ByteArrayInputStream(img.getBytes()));
} else {
BufferedImage buffered = ImageIO.read(new ByteArrayInputStream(
img.getBytes()));

xobject = LosslessFactory.createFromImage(_writer, buffered);
}
xobject = PDImageXObject.createFromByteArray(_writer, img.getBytes(), img.getUri());
} catch (IOException e) {
throw new PdfContentStreamAdapter.PdfException("realizeImage", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ImageResource getImageResource(String uriStr) {
if (resource != null && resource.getImage() instanceof PdfBoxImage) {
// Make copy of PdfBoxImage so we don't stuff up the cache.
PdfBoxImage original = (PdfBoxImage) resource.getImage();
PdfBoxImage copy = new PdfBoxImage(original.getBytes(), original.getUri(), original.getWidth(), original.getHeight(), original.isJpeg(), original.getXObject());
PdfBoxImage copy = new PdfBoxImage(original.getBytes(), original.getUri(), original.getWidth(), original.getHeight(), original.getXObject());
return new ImageResource(resource.getImageUri(), copy);
}

Expand Down

0 comments on commit 1290eba

Please sign in to comment.