【问题标题】:PrintedPDFDocument results in PDF containing Grey box with sort of Grid LayoutPrintedPDFDocument 生成的 PDF 包含带有网格布局的灰色框
【发布时间】:2015-03-13 23:17:32
【问题描述】:

经过许多问题,我终于能够使用 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>

这会导致这样的用户界面:

我用来生成 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 的链接,以防提供任何有用的信息:https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf

【问题讨论】:

  • 你实际上是在哪里渲染视图?
  • 视图在初始活动片段中呈现,当用户单击按钮时,视图随后被绘制为 pdf。
  • 你需要展示你的整个代码。
  • 尝试删除 PageInfo 的东西。您不需要它,因为您只有 1 页,并且您已经描述了您想要的页面大小是 ISO_A4。此外,您需要显示salesFragmentTableLayout.draw(page.getCanvas()); 在做什么,因为这可能是您只有一个灰色框的罪魁祸首。

标签: android pdf pdf-generation


【解决方案1】:

根据documentation:

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

使用必需的页面信息属性创建一个新构建器。

参数

pageWidth PostScript 中的页面宽度(1/72 英寸)。

pageHeight PostScript 中的页面高度(1/72 英寸)。

pageNumber页码。

你使用一个

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

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

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

Page page = document.startPage(1);

(再次参见the documentation)

【讨论】:

  • 也许这个link 可以提供帮助
猜你喜欢
  • 2020-10-18
  • 2021-11-23
  • 1970-01-01
  • 2013-01-11
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
相关资源
最近更新 更多