我尝试编写一个简单的方法,从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>
我想这只是一些愚蠢的东西,我错过了或做错了 - 但我自己也看不到。有什么想法吗?
答案 0 :(得分:0)
我现在已经从应用程序本身运行此代码,而不是从测试中运行。 PDF不再是0字节 - 它已被破坏,但这是一个不同的问题。
看起来膨胀中没有父母是导致0字节问题的原因 - 我不知道在诉讼的那个阶段我从哪里获得有效的父母。
答案 1 :(得分: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;
}