【发布时间】:2014-09-03 12:54:24
【问题描述】:
我尝试编写一个简单的方法,该方法从 Android 布局中获取视图并使用 PdfDocument 类将其转换为 PDF。由于某种原因,生成的 PDF 始终为 0 字节。
这是我生成 PDF 的代码。稍后在应用程序中,我将以编程方式将其附加到电子邮件中,因此我目前将其作为 File 对象获取:
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>
我想这只是我错过或做错了一些愚蠢的事情 - 但我自己看不到。有什么想法吗?
【问题讨论】:
-
你最终解决了这个问题吗?我有同样的问题。
-
对我来说,这是由运行单元测试中的代码引起的。当我在应用程序本身中运行它时它起作用了