我試過編寫一個簡單的方法,該方法從Android佈局獲取視圖並使用PdfDocument類將其轉換爲PDF。出於某種原因,生成的PDF總是0字節。在android中通過PdfDocument創建的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>
我想象它只是一些愚蠢的我失蹤或者做錯了 - 但我不能看到它自己。有任何想法嗎?
你最終解決了這個問題嗎?我有同樣的問題。 – user1163234 2015-10-11 12:59:34
對我來說,這是由單元測試運行代碼引起的。它運行時,我在應用程序本身運行它 – 2015-10-11 14:36:35