[
https://issues.apache.org/jira/browse/PDFBOX-2616?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
John Hewson resolved PDFBOX-2616.
---------------------------------
Resolution: Won't Fix
Fix Version/s: 2.0.0
This is a known problem, it is fixed in 2.0 by simply avoiding the broken JVM
code. No fix is possible for 1.8.
> JVM crashes while trying to convert PDF to JPG image (only on Windows)
> ----------------------------------------------------------------------
>
> Key: PDFBOX-2616
> URL: https://issues.apache.org/jira/browse/PDFBOX-2616
> Project: PDFBox
> Issue Type: Bug
> Affects Versions: 1.8.8
> Environment: - Windows 7
> - Java SDK 1.6.0 (update 45)
> Reporter: Jochen Hebbrecht
> Fix For: 2.0.0
>
> Attachments: screenshot-1.png
>
>
> Hi,
> We got the following exception when trying to convert a PDF to an image (see
> attachment).
> {code}
> Assertion failed!
> .../src/share/native/sun/font/t2k/truetype.c
> Line: 1335
> {code}
> Our JVM actually crashed as well and is no longer running.
> Our code to convert the PDF to image:
> {code}
> public static void generateThumbnail(InputStream is, FileExtension
> fileExtension, OutputStream os, Hashtable<String, Object> attributes) throws
> OneaException {
> // Create JPEG writer
> ImageWriter writer = null;
>
> try {
> // Load the attributes
> Integer size = 400;
> if (attributes.containsKey(ATTRIBUTE_SIZE)) {
> try {
> size = (Integer)
> attributes.get(ATTRIBUTE_SIZE);
> } catch (Exception ex) {
> throw new OneaException("Error while
> loading the '" + ATTRIBUTE_SIZE + "' attribute");
> }
> }
> Float quality = 0.75f;
> if (attributes.containsKey(ATTRIBUTE_QUALITY)) {
> try {
> quality = (Float)
> attributes.get(ATTRIBUTE_QUALITY);
> } catch (Exception ex) {
> throw new OneaException("Error while
> loading the '" + ATTRIBUTE_QUALITY + "' attribute");
> }
> }
> Boolean color = false;
> if (attributes.containsKey(ATTRIBUTE_COLOR)) {
> try {
> color = (Boolean)
> attributes.get(ATTRIBUTE_COLOR);
> } catch (Exception ex) {
> throw new OneaException("Error while
> loading the '" + ATTRIBUTE_COLOR + "' attribute");
> }
> }
>
> // Load the image
> BufferedImage img = loadImage(is, fileExtension);
>
> // To grayscale
> if (color == null || !color) {
> BufferedImage grayImg = new
> BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
> Graphics g = grayImg.getGraphics();
> g.drawImage(img, 0, 0, null);
> g.dispose();
> img = grayImg;
> }
>
> // Scale the image
> img = scaleImage(img, size, size);
>
> ImageWriteParam writerParams = null;
> try {
> JPEGImageWriterSpi jpegspi = new
> JPEGImageWriterSpi();
> writer = jpegspi.createWriterInstance();
> writerParams = writer.getDefaultWriteParam();
>
> writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
> writerParams.setCompressionQuality(quality);
> } catch (Exception ex) {
> throw new OneaException("Could not load the
> JPEG writer", ex);
> }
>
> // Write the image
> writer.setOutput(ImageIO.createImageOutputStream(os));
> writer.write(null, new IIOImage(img, null, null),
> writerParams);
> } catch (Exception ex) {
> throw new OneaException(ex.getMessage() == null ?
> "Error while writing the JPG thumbnail" : ex.getMessage(), ex);
> } finally {
> StreamTools.closeStream(is);
> StreamTools.closeStream(os);
> if (writer != null)
> writer.dispose();
> }
> }
>
> // Helper methods
>
> private static BufferedImage loadImage(InputStream is, FileExtension
> fileExtension) throws OneaException {
> if (fileExtension == null)
> throw new OneaException("No file type passed");
>
> switch (fileExtension) {
> case PDF:
> PDDocument document = null;
> try {
> // Load the PDF file
> document = PDDocument.load(is);
> PDPage page = (PDPage)
> document.getDocumentCatalog().getAllPages().get(0);
>
> // Fetches conversion images
> PDResources resources =
> page.getResources();
> Map<String, PDXObjectImage> images =
> resources.getImages();
> if (images != null) {
> for (String key :
> images.keySet()) {
> PDXObjectImage image =
> images.get(key);
> if (image.getRGBImage()
> == null) {
> throw new
> OneaException("No RGBImage found (check conversion)");
> }
> }
> }
>
> // Convert the first page to an image
> return page.convertToImage();
>
> } catch (Exception ex) {
> throw new OneaException(ex.getMessage()
> == null ? "Error while decoding PDF" : ex.getMessage(), ex);
> } finally {
> try {
> if (document != null)
> document.close();
> } catch (Exception ex) {
> }
> }
> case TIF:
> case TIFF:
> case PNG:
> case JPEG:
> case GIF:
> ImageInputStream iis = null;
> try {
> iis =
> ImageIO.createImageInputStream(is);
> Iterator<ImageReader> readers =
> ImageIO.getImageReaders(iis);
> if (!readers.hasNext())
> throw new OneaException("Cannot
> load '" + fileExtension.toString() + "' reader");
> // Get TIFF reader, and set input
> ImageReader reader = readers.next();
> reader.setInput(iis);
>
> return reader.read(0, null);
> } catch (Exception ex) {
> throw new OneaException("Error while
> decoding '" + fileExtension.toString() + "'", ex);
> } finally {
> if (iis != null)
> try {
> iis.close();
> } catch (Exception ex) {
> }
> }
> default:
> throw new OneaException("Cannot generate the
> thumbnail image, unknown file type");
> }
>
> }
>
> private static BufferedImage scaleImage(BufferedImage img, int
> maxWidth, int maxHeight) throws OneaException {
> Double w = new Double(img.getWidth());
> Double h = new Double(img.getHeight());
>
> // Calculate scale factor
> Double f1 = new Double(maxHeight) / h;
> Double f2 = new Double(maxWidth) / w;
> Double factor = f1 > f2 ? f2 : f1;
> w = w * factor;
> h = h * factor;
>
> // Scale the image
> BufferedImage thumb = new BufferedImage(w.intValue(),
> h.intValue(), img.getType());
> AffineTransform at = new AffineTransform();
> at.scale(factor, factor);
> thumb = new AffineTransformOp(at,
> AffineTransformOp.TYPE_BILINEAR).filter(img, thumb);
>
> return thumb;
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)