PrintedPDFDocument 导致 PDF 包含带有网格布局的灰色框

PrintedPDFDocument results in PDF containing Grey box with sort of Grid Layout

经过许多问题,我终于能够使用 PrintedPDFClass 生成具有内容且未损坏的 PDF 文档。

然而,问题是 PDF 中的内容似乎与我给它呈现的视图没有任何关系...

我要渲染的视图:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:kingdomspas="http://www.kingdomspas.com/android/custom"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

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

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"></TableLayout>

            <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" />

                <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1">
                <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" />

                    <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>

                </LinearLayout>
            </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>

        <View
            android:paddingTop="50dp"
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" />

        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/submitButton"
            android:text="@string/submit_form"
            android:padding="10dp"/>
    </LinearLayout>

</ScrollView>

这导致 UI 像这样:

我用来生成 PDF 的代码是:

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);
        Canvas pdfCanvas = page.getCanvas();
        salesFragmentTableLayout.draw(page.getCanvas());
        document.finishPage(page);
        File result = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos= null;
        FileDescriptor descriptor = null;
        try {
            result = File.createTempFile("Kingdom Spas Agreement", ".pdf", context.getCacheDir());
            fos = new FileOutputStream(result);
            bos = new BufferedOutputStream(fos);
            document.writeTo(bos);
            descriptor = fos.getFD();
            descriptor.sync();
        } 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);
        } finally {
            try {
                if (bos != null) { bos.flush(); bos.close(); }
            } catch (SyncFailedException e) {
                throw new KingdomSpasException("Failed to correctly sync PDF file - may be corrupted", e);
            } catch (IOException e) {
                throw new KingdomSpasException("Failed to correctly clean up streams", e);
            }
        }
        document.close();

但创建的 PDF 看起来像(以 100% 缩放查看):

任何人都可以向我解释为什么生成的 PDf(至少在我看来)与我的原始视图如此不同并解释如何修复它?

编辑:这里是生成的 PDF 的 link,以防提供任何有用的信息:https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf

根据 documentation:

public PdfDocument.PageInfo.Builder (int pageWidth, int pageHeight, int pageNumber)

Creates a new builder with the mandatory page info attributes.

Parameters

pageWidth The page width in PostScript (1/72th of an inch).

pageHeight The page height in PostScript (1/72th of an inch).

pageNumber The page number.

您使用一个

PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
Page page = document.startPage(pageInfo);

因此,您创建的页面大小仅为 2 英寸 x 2 英寸。在这个小区域中,只有一部分提交按钮适合。 (PDF 页面内容实际上甚至包含字符串“Submit”,但它远远超出了可视区域。)

当您使用 PrintedPdfDocument 时,您应该改用:

Page page = document.startPage(1);

(cf. the documentation again)