Android 从布局视图创建和打印 pdf
Posted
技术标签:
【中文标题】Android 从布局视图创建和打印 pdf【英文标题】:Android create and print pdf from layout view 【发布时间】:2017-09-27 17:16:02 【问题描述】:我正在尝试从 xml 布局视图创建 PDF 文件。 我在该布局中有一个列表视图,添加项目并基于子级设置高度。 PDF 正在创建但未填满整个页面。 我试过的是,
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View content = inflater.inflate(R.layout.pdf_layout, null);
content.measure(2250, 1400);
content.layout(0,0, 2250, 1400);
tvName = (TextView)content.findViewById(R.id.tvName);
tvDate = (TextView)content.findViewById(R.id.tvDate);
tvAge = (TextView)content.findViewById(R.id.tvAge);
tvGender = (TextView)content.findViewById(R.id.tvGender);
tvPhone = (TextView)content.findViewById(R.id.tvPhone);
lvList = (ListView)content.findViewById(R.id.lvList);
lvList.setAdapter(adapter);
Utils.setListViewHeight(lvList, CreatePDFDemo.this);
tvName.setText(name);
tvAge.setText(age + "Y");
tvGender.setText(gender);
tvPhone.setText(phone);
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// add more pages
// write the document content
try
document.writeTo(output);
catch (IOException e)
e.printStackTrace();
它的输出就像这张图片,
如何编写覆盖整个 pdf 页面宽度的布局视图?
【问题讨论】:
【参考方案1】:改成这样,
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
content.measure(measureWidth, measuredHeight);
content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
这将获得页面的全高和全宽。
【讨论】:
【参考方案2】:使用 [打印内容] (https://developer.android.com/reference/android/support/v4/print/PrintHelper.html)!
// Get the print manager.
PrintHelper printHelper = new PrintHelper(this);
// Set the desired scale mode.
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
// Get the bitmap for the ImageView's drawable.
Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
// Print the bitmap.
printHelper.printBitmap("Print Bitmap", bitmap);
【讨论】:
它只打印一页,如果布局超过一页长度如何解决这个问题?【参考方案3】:我创建了一个library 来实现这个目标(从布局视图中获取 PDF)。
主要代码 sn-p 带有正确的文档-
PdfGenerator.getBuilder()
.setContext(context)
.fromLayoutXMLSource()
.fromLayoutXML(R.layout.layout_print,R.layout.layout_print)
/* "fromLayoutXML()" takes array of layout resources.
* You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */
.setDefaultPageSize(PdfGenerator.PageSize.A4)
/* It takes default page size like A4,A5. You can also set custom page size in pixel
* by calling ".setCustomPageSize(int widthInPX, int heightInPX)" here. */
.setFileName("Test-PDF")
/* It is file name */
.setFolderName("FolderA/FolderB/FolderC")
/* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then
* FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally
* the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */
.openPDFafterGeneration(true)
/* It true then the generated pdf will be shown after generated. */
.build(new PdfGeneratorListener()
@Override
public void onFailure(FailureResponse failureResponse)
super.onFailure(failureResponse);
/* If pdf is not generated by an error then you will findout the reason behind it
* from this FailureResponse. */
@Override
public void showLog(String log)
super.showLog(log);
/*It shows logs of events inside the pdf generation process*/
@Override
public void onSuccess(SuccessResponse response)
super.onSuccess(response);
/* If PDF is generated successfully then you will find SuccessResponse
* which holds the PdfDocument,File and path (where generated pdf is stored)*/
);
【讨论】:
【参考方案4】:尝试将您的布局转换为图像,然后将该图像设置为 PDF。读到这里,也许你会有所了解。 Convert view to PDF
【讨论】:
以上是关于Android 从布局视图创建和打印 pdf的主要内容,如果未能解决你的问题,请参考以下文章