Author: ssteiner Date: Mon Jun 29 12:18:29 2020 New Revision: 1879324 URL: http://svn.apache.org/viewvc?rev=1879324&view=rev Log: FOP-2949: Allow preload of pdf with extra data before header
Added: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java (with props) Modified: xmlgraphics/fop-pdf-images/trunk/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PreloaderPDFTestCase.java Modified: xmlgraphics/fop-pdf-images/trunk/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader?rev=1879324&r1=1879323&r2=1879324&view=diff ============================================================================== --- xmlgraphics/fop-pdf-images/trunk/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader (original) +++ xmlgraphics/fop-pdf-images/trunk/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader Mon Jun 29 12:18:29 2020 @@ -1,2 +1,3 @@ org.apache.fop.render.pdf.pdfbox.PreloaderPDF -org.apache.fop.render.pdf.pdfbox.PreloaderImageRawData \ No newline at end of file +org.apache.fop.render.pdf.pdfbox.PreloaderImageRawData +org.apache.fop.render.pdf.pdfbox.LastResortPreloaderPDF \ No newline at end of file Added: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java?rev=1879324&view=auto ============================================================================== --- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java (added) +++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java Mon Jun 29 12:18:29 2020 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ +package org.apache.fop.render.pdf.pdfbox; + +import java.io.IOException; + +import javax.imageio.stream.ImageInputStream; + +/** + * Last resort PDF preloader for PDFs with data before the header + */ +public class LastResortPreloaderPDF extends PreloaderPDF { + protected byte[] getHeader(ImageInputStream in, int size) throws IOException { + byte[] header = new byte[1024]; + long startPos = in.getStreamPosition(); + int len = in.read(header); + in.seek(startPos); + String s = new String(header, "US-ASCII"); + if (len > 0 && s.contains(PDF_HEADER)) { + return PDF_HEADER.getBytes("US-ASCII"); + } + return new byte[0]; + } + + public int getPriority() { + return DEFAULT_PRIORITY * 3; + } +} Propchange: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/LastResortPreloaderPDF.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java?rev=1879324&r1=1879323&r2=1879324&view=diff ============================================================================== --- xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java (original) +++ xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PreloaderPDF.java Mon Jun 29 12:18:29 2020 @@ -48,7 +48,7 @@ import org.apache.fop.render.pdf.pdfbox. public class PreloaderPDF extends AbstractImagePreloader { /** PDF header text */ - private static final String PDF_HEADER = "%PDF-"; + protected static final String PDF_HEADER = "%PDF-"; /** {@inheritDoc} */ public ImageInfo preloadImage(String uri, Source src, ImageContext context) Modified: xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PreloaderPDFTestCase.java URL: http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PreloaderPDFTestCase.java?rev=1879324&r1=1879323&r2=1879324&view=diff ============================================================================== --- xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PreloaderPDFTestCase.java (original) +++ xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PreloaderPDFTestCase.java Mon Jun 29 12:18:29 2020 @@ -21,15 +21,18 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; +import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.MemoryCacheImageInputStream; import org.junit.Assert; import org.junit.Test; +import org.apache.commons.io.IOUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.xmlgraphics.image.loader.ImageException; @@ -39,11 +42,10 @@ import org.apache.xmlgraphics.image.load import org.apache.xmlgraphics.image.loader.impl.ImageRendered; import org.apache.fop.render.pdf.pdfbox.ImagePDF; +import org.apache.fop.render.pdf.pdfbox.LastResortPreloaderPDF; import org.apache.fop.render.pdf.pdfbox.PreloaderImageRawData; import org.apache.fop.render.pdf.pdfbox.PreloaderPDF; - - public class PreloaderPDFTestCase { @Test @@ -84,4 +86,16 @@ public class PreloaderPDFTestCase { doc.save(new ByteArrayOutputStream()); img.close(); } + + @Test + public void testLastResortPreloaderPDF() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + bos.write("xx".getBytes("UTF-8")); + IOUtils.copy(new FileInputStream(PDFBoxAdapterTestCase.ROTATE), bos); + InputStream pdf = new ByteArrayInputStream(bos.toByteArray()); + ImageInputStream inputStream = ImageIO.createImageInputStream(pdf); + ImageSource imageSource = new ImageSource(inputStream, "", true); + ImageInfo imageInfo = new LastResortPreloaderPDF().preloadImage("", imageSource, new DefaultImageContext()); + Assert.assertEquals(imageInfo.getMimeType(), "application/pdf"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: fop-commits-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-commits-h...@xmlgraphics.apache.org