|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.renderer.pdf.pdfbox; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +import java.io.InputStream; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.apache.tika.io.TikaInputStream; |
| 28 | +import org.apache.tika.metadata.Metadata; |
| 29 | +import org.apache.tika.parser.ParseContext; |
| 30 | +import org.apache.tika.parser.pdf.PDFParserConfig; |
| 31 | +import org.apache.tika.renderer.PageBasedRenderResults; |
| 32 | +import org.apache.tika.renderer.PageRangeRequest; |
| 33 | +import org.apache.tika.renderer.RenderResult; |
| 34 | + |
| 35 | +public class PDFBoxRendererTest { |
| 36 | + |
| 37 | + private long renderedPngBytes(ParseContext context) throws Exception { |
| 38 | + PDFBoxRenderer renderer = new PDFBoxRenderer(); |
| 39 | + try (InputStream is = getClass().getResourceAsStream("/test-documents/testPDF.pdf"); |
| 40 | + TikaInputStream tis = TikaInputStream.get(is)) { |
| 41 | + assertNotNull(is); |
| 42 | + PageBasedRenderResults results = (PageBasedRenderResults) renderer.render( |
| 43 | + tis, new Metadata(), context, new PageRangeRequest(1, 1)); |
| 44 | + RenderResult r = results.getResults().get(0); |
| 45 | + assertEquals(RenderResult.STATUS.SUCCESS, r.getStatus()); |
| 46 | + try (TikaInputStream img = r.getInputStream()) { |
| 47 | + byte[] b = img.readAllBytes(); |
| 48 | + assertEquals((byte) 0x89, b[0]); |
| 49 | + assertEquals((byte) 'P', b[1]); |
| 50 | + results.close(); |
| 51 | + return b.length; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testPngCompressedByDefault() throws Exception { |
| 58 | + // letter page, 300 dpi gray: raw raster ~8.5 MB; compressed must be far smaller |
| 59 | + long bytes = renderedPngBytes(new ParseContext()); |
| 60 | + assertTrue(bytes < 2_000_000, "default render should be a compressed PNG, got " + bytes); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testImageQualityConfigurable() throws Exception { |
| 65 | + // ImageIO's PNG "quality" 1.0 = uncompressed; proves the config reaches the writer |
| 66 | + PDFParserConfig config = new PDFParserConfig(); |
| 67 | + config.getOcr().setImageQuality(1.0f); |
| 68 | + ParseContext context = new ParseContext(); |
| 69 | + context.set(PDFParserConfig.class, config); |
| 70 | + long uncompressed = renderedPngBytes(context); |
| 71 | + long compressed = renderedPngBytes(new ParseContext()); |
| 72 | + assertTrue(uncompressed > compressed * 4, |
| 73 | + "quality 1.0 should be far larger: " + uncompressed + " vs " + compressed); |
| 74 | + } |
| 75 | +} |
0 commit comments