PrintedPdfDocument始终导致PDF损坏

时间:2014-09-18 12:18:59

标签: android pdf pdf-generation

我正在尝试根据用户在我的Android应用中显示的表单创建PDF文档。我使用以下代码创建PDF文件,然后将其附加到电子邮件中:

public static File generate(View salesFragmentTableLayout, Context context) throws KingdomSpasException {
        Builder printAttrsBuilder = new Builder();
        printAttrsBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
        printAttrsBuilder.setMinMargins(new Margins(5, 5, 5, 5));

        PrintedPdfDocument document = new PrintedPdfDocument(context, printAttrsBuilder.build());

        PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
        Page page = document.startPage(pageInfo);
        salesFragmentTableLayout.draw(page.getCanvas());
        document.finishPage(page);
        File result = null;
        try {
            result = File.createTempFile("Kingdom Spas Agreement", ".pdf", context.getCacheDir());
            document.writeTo(new BufferedOutputStream(new FileOutputStream(result)));
        } catch (FileNotFoundException e) {
            throw new KingdomSpasException("Failed to find relevent file", e);
        } catch (IOException e) {
            throw new KingdomSpasException("IO Problem occured while creatin the PDF", e);
        }
        document.close();
        return result;
    }

生成的PDF始终已损坏,无法通过Adobe Acroread或GS打开。当我在acroread中打开它时,我收到错误:

  

打开此文档时出错。文件已损坏,无法修复。

当我尝试使用以下命令在GS中打开它时:

gs \
   -o repaired.pdf \
   -sDEVICE=pdfwrite \
   -dPDFSETTINGS=/prepress \
  KingdomSpasAgreement.pdf 

我得到以下输出:

  

****错误:无法在文件中的任何位置找到“startxref”   ****警告:读取XREF表时发生错误   ****文件已损坏。这可能是由于造成的   转换或转移文件时出现问题****   **** Ghostscript将尝试恢复数据   ****错误:未找到预告片。不会处理任何页面(FirstPage> LastPage)。

     

****此文件包含已修复或忽略的错误   ****请通知作者产生此软件的软件   ****文件,它不符合Adobe发布的PDF
  ****规范。

我不明白我做错了什么 - 整个过程看起来相当简单,但总是失败。

编辑:

以下是完整性的相关布局:

<TableLayout
            android:id="@+id/salesAgreementTableLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/companyLogo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/logo_description"
                    android:src="@drawable/company_logo" />

                <ImageView
                    android:id="@+id/companyAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/address_description"
                    android:src="@drawable/company_address" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <EditText
                        android:id="@+id/salesExecInitials"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/round"
                        android:inputType="text"
                        android:maxLines="1"
                        android:paddingLeft="130dp"
                        android:singleLine="true" >

                        <requestFocus />
                    </EditText>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:text="@string/sales_exec_initials" />
                </FrameLayout>
            </TableRow>
        </TableLayout>

编辑#2:我在通过电子邮件发送后检查了文件,但现在使用“adb pull”直接检查了文件,但它仍然以同样的方式损坏。

编辑#3:我已将一个损坏的PDF示例上传到dropox:Example of Corrupted PDF

它实际上使用的是更简化的视图,但仍然是损坏的

编辑#4:我现在尝试在getFD()上调用sync(),并按照CommonsWare下面的建议关闭流,这会导致异常,我觉得这很有趣。堆栈跟踪在这里,以防它脱落任何光:

12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950): com.kingdomspas.android.kingdomspasforms.exceptions.KingdomSpasException: Failed to correctly clean up streams
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.utils.KingdomSpasFormsPDFGenerator.generate(KingdomSpasFormsPDFGenerator.java:69)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.listeners.KingdomSpasSubmitButtonListener.onClick(KingdomSpasSubmitButtonListener.java:25)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.view.View.performClick(View.java:4438)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.view.View$PerformClick.run(View.java:18439)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Handler.handleCallback(Handler.java:733)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Handler.dispatchMessage(Handler.java:95)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Looper.loop(Looper.java:136)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at android.app.ActivityThread.main(ActivityThread.java:5147)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at java.lang.reflect.Method.invoke(Method.java:515)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at dalvik.system.NativeStart.main(Native Method)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950): Caused by: java.io.IOException: write failed: EBADF (Bad file number)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.IoBridge.write(IoBridge.java:455)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at java.io.FileOutputStream.write(FileOutputStream.java:187)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.utils.KingdomSpasFormsPDFGenerator.generate(KingdomSpasFormsPDFGenerator.java:63)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   ... 12 more
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950): Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.Posix.writeBytes(Native Method)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.Posix.write(Posix.java:202)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.IoBridge.write(IoBridge.java:450)
12-16 16:39:05.675: E/KingdomSpasSubmitButtonListener(17950):   ... 16 more
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950): Failed to generate the agreement PDF
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950): com.kingdomspas.android.kingdomspasforms.exceptions.KingdomSpasException: Failed to correctly clean up streams
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.utils.KingdomSpasFormsPDFGenerator.generate(KingdomSpasFormsPDFGenerator.java:69)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.listeners.KingdomSpasSubmitButtonListener.onClick(KingdomSpasSubmitButtonListener.java:25)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.view.View.performClick(View.java:4438)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.view.View$PerformClick.run(View.java:18439)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Handler.handleCallback(Handler.java:733)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Handler.dispatchMessage(Handler.java:95)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.os.Looper.loop(Looper.java:136)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at android.app.ActivityThread.main(ActivityThread.java:5147)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at java.lang.reflect.Method.invoke(Method.java:515)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at dalvik.system.NativeStart.main(Native Method)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950): Caused by: java.io.IOException: write failed: EBADF (Bad file number)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.IoBridge.write(IoBridge.java:455)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at java.io.FileOutputStream.write(FileOutputStream.java:187)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at com.kingdomspas.android.kingdomspasforms.utils.KingdomSpasFormsPDFGenerator.generate(KingdomSpasFormsPDFGenerator.java:63)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   ... 12 more
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950): Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.Posix.writeBytes(Native Method)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.Posix.write(Posix.java:202)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   at libcore.io.IoBridge.write(IoBridge.java:450)
12-16 16:39:13.845: E/KingdomSpasSubmitButtonListener(17950):   ... 16 more

2 个答案:

答案 0 :(得分:5)

您永远不会flush()close()您的信息流。您还应该在sync()上的getFD()上调用FileOutputStream以确保所有字节都提交到磁盘(而不是由文件系统写入缓存)。根据您尝试对文件执行某些操作的时间,此类内容可能会导致截断输出。

答案 1 :(得分:0)

我已经测试了您的代码,它总是创建一个空文档(0个内容字节),而不是一个损坏的文档,因此这解释了为什么您无法使用gs修复它。 关于Android打印框架,我不确定您是否可以在不调用PrintManager类的情况下直接打印文档。我读过的所有文档都提到了它,我见过的所有示例代码都使用它。

在Android开发者网站上打印自定义文档有good tutorial,您可以查看。还提供了一个完整的示例here。 我已经测试了最后一个示例代码并且运行良好!唯一的区别是它在打印前显示了一些用于选择选项的UI。

希望你能这样做!