TomRoush / PdfBox-Android

The Apache PdfBox project ported to work on Android
Apache License 2.0
995 stars 260 forks source link

PdfBox-Android: Missing color space: Pattern #248

Open bichao1986 opened 3 years ago

bichao1986 commented 3 years ago

This problem occurs in the process of converting a relatively complex PDF into an image. For example, my PDF contains a large number of images and Chinese fonts. If it is a relatively simple PDF, there will be no exception log info: 2021-03-15 10:59:53.347 12209-12209/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tom_roush.pdfbox.sample, PID: 12209 java.lang.IllegalStateException: Could not execute method for android:onClick at android.view.View$DeclaredOnClickListener.onClick(View.java:6118) at android.view.View.performClick(View.java:7281) at android.view.View.performClickInternal(View.java:7255) at android.view.View.access$3600(View.java:828) at android.view.View$PerformClick.run(View.java:27924) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8387) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:6113) at android.view.View.performClick(View.java:7281)  at android.view.View.performClickInternal(View.java:7255)  at android.view.View.access$3600(View.java:828)  at android.view.View$PerformClick.run(View.java:27924)  at android.os.Handler.handleCallback(Handler.java:900)  at android.os.Handler.dispatchMessage(Handler.java:103)  at android.os.Looper.loop(Looper.java:219)  at android.app.ActivityThread.main(ActivityThread.java:8387)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)  Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at com.tom_roush.pdfbox.pdmodel.graphics.color.PDDeviceGray.toRGB(PDDeviceGray.java:72) at com.tom_roush.pdfbox.rendering.PageDrawer.getColor(PageDrawer.java:261) at com.tom_roush.pdfbox.rendering.PageDrawer.getNonStrokingColor(PageDrawer.java:540) at com.tom_roush.pdfbox.rendering.PageDrawer.fillPath(PageDrawer.java:610) at com.tom_roush.pdfbox.contentstream.operator.graphics.FillEvenOddRule.process(FillEvenOddRule.java:37) at com.tom_roush.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:798) at com.tom_roush.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:460) at com.tom_roush.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:437) at com.tom_roush.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:146) at com.tom_roush.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:171) at com.tom_roush.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:167) at com.tom_roush.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:129) at com.tom_roush.pdfbox.sample.MainActivity.renderFile(MainActivity.java:164) at java.lang.reflect.Method.invoke(Native Method)  at android.view.View$DeclaredOnClickListener.onClick(View.java:6113)  at android.view.View.performClick(View.java:7281)  at android.view.View.performClickInternal(View.java:7255)  at android.view.View.access$3600(View.java:828)  at android.view.View$PerformClick.run(View.java:27924)  at android.os.Handler.handleCallback(Handler.java:900)  at android.os.Handler.dispatchMessage(Handler.java:103)  at android.os.Looper.loop(Looper.java:219)  at android.app.ActivityThread.main(ActivityThread.java:8387)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)  2021-03-15 10:59:53.357 12209-12209/? I/Process: Sending signal. PID: 12209 SIG: 9

TomRoush commented 3 years ago

PdfBox-Android doesn't currently support the Pattern colorspace. Your complex PDF is using that colorspace somewhere and your simple PDF isn't. However, if there's an unsupported colorspace, the expected behavior is that it will log the missing colorspace and move on, not crash your app. If you're able to share the PDF that's failing that would help with debugging.

bichao1986 commented 3 years ago

Thanks,I have solved this problem by debugging,modify PageDrawer.java code is this:

private int getColor(PDColor color) throws IOException {
        PDColorSpace colorSpace = color.getColorSpace();
        float[] cvalue = color.getComponents();
        if (cvalue.length>0){
            float[] floats = colorSpace.toRGB(cvalue);
            int r = Math.round(floats[0] * 255);
            int g = Math.round(floats[1] * 255);
            int b = Math.round(floats[2] * 255);
            return Color.rgb(r, g, b);
        }else{
            int r = Math.round( 255);
            int g = Math.round(255);
            int b = Math.round(255);
            return Color.rgb(r, g, b);
        }

    }
hanneluca commented 3 years ago

Is there a plan for adding this fix in the next version?

TomRoush commented 3 years ago

Reopening, missing color spaces should be ignored, they shouldn't cause Exceptions