Skip to content

Use the PDFBox builtin image direct embedding #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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