Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PdfDocument在android中创建的PDF为0字节_Android_Pdf Generation - Fatal编程技术网

通过PdfDocument在android中创建的PDF为0字节

通过PdfDocument在android中创建的PDF为0字节,android,pdf-generation,Android,Pdf Generation,我尝试编写一个简单的方法,从Android布局中获取视图,并使用PdfDocument类将其转换为PDF。由于某些原因,生成的PDF始终为0字节 这是我生成PDF的代码。稍后在应用程序中,我将以编程方式将其附加到电子邮件,因此我当前将其作为文件对象: public static File generate(View salesFragmentTableLayout, Context context) throws KingdomSpasException { PdfDocumen

我尝试编写一个简单的方法,从Android布局中获取视图,并使用PdfDocument类将其转换为PDF。由于某些原因,生成的PDF始终为0字节

这是我生成PDF的代码。稍后在应用程序中,我将以编程方式将其附加到电子邮件,因此我当前将其作为文件对象:

public static File generate(View salesFragmentTableLayout, Context context) throws KingdomSpasException {
        PdfDocument document = new PdfDocument();
        PageInfo pageInfo = new PageInfo.Builder(300, 300, 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;
    }
这是目前从我的单元测试中调用的,我还没有将它集成到实际应用程序中。万一问题是由我的测试代码引起的,我也将其添加到这里:

public void testGetPDFFile() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View salesAgreementLayout = inflater.inflate(R.layout.sales_agreement_layout, null);
        View salesAgreementTableLayout = salesAgreementLayout.findViewById(R.id.salesAgreementTableLayout);
        EditText initials = (EditText) salesAgreementTableLayout.findViewById(R.id.salesExecInitials);
        initials.setText("Test Initials");
        File pdfFile = null;
        try {
            pdfFile = KingdomSpasFormsPDFGenerator.generate(salesAgreementTableLayout, getContext());
        } catch (KingdomSpasException e) {
            Log.e(TAG, "Failed to create the PDF file.",e);

        }
        assertNotNull(pdfFile);
        assertTrue(pdfFile.length() > 0);
    }
第二个断言当前失败

以下是活动中使用的完整布局:

<?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:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:id="@+id/salesAgreementTableLayout">

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

        <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/submit"
            android:text="@string/submit_form"
            android:padding="10dp"/>
    </LinearLayout>

</ScrollView>

我想这只是一件愚蠢的事情,我错过了或做错了——但我自己却看不出来。有什么想法吗?

我现在已经从应用程序本身而不是从测试中运行了这段代码。PDF不再是0字节-它已损坏,但这是另一个问题

似乎在充气过程中没有父级是导致0字节问题的原因-我不知道在诉讼的那个阶段从哪里可以获得有效的父级。

尝试添加这行代码,然后再试一次。 我认为我们需要设置视图的宽度和高度,然后再将其绘制到画布中

    public static File generate(View salesFragmentTableLayout, Context context) throws KingdomSpasException {
    PdfDocument document = new PdfDocument();
    PageInfo pageInfo = new PageInfo.Builder(300, 300, 1).create();
    Page page = document.startPage(pageInfo);

    // Try adding these lines
    // draw view on the page
    int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
    salesFragmentTableLayout.measure(measureWidth, measuredHeight);
    salesFragmentTableLayout.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

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

你最终解决了这个问题吗?我也有同样的问题。对我来说,这是由运行单元测试的代码引起的。当我自己在应用程序中运行它时,它工作了。你最终解决了这个问题吗?我也有同样的问题。