PDF are simple document format with supports text and images. It is simple to share, non editable can be password protected. Great for receipts and invoices.
As i was looking for a library to create PDF natively on Android but couldn't found one that is free to use.
Some time ago, i had a use case where i needed to create PDF on device and unfortunately no free to use library was available at that time which can create PDF's on user's device.
So, i digged into Android's Printing custom documents where i came across PdfDocument which can generate PDF from View
.
Here is how it is done:
We start by creating a PdfDocument
// Create PDF Document. PdfDocument pdfDocument = new PdfDocument();
Create a Page in PdfDocument
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo. Builder((int) PDF_PAGE_WIDTH, (int) PDF_PAGE_HEIGHT, i + 1).create();
Create a view to be printed on PDF
TextView view = new TextView(context); view.setText("PDF Example");
Draw view on the page
Canvas pageCanvas = page.getCanvas(); pageCanvas.scale(1f, 1f); int pageWidth = pageCanvas.getWidth(); int pageHeight = pageCanvas.getHeight(); int measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY); contentView.measure(measureWidth, measuredHeight); contentView.layout(0, 0, pageWidth, pageHeight); contentView.draw(pageCanvas);
Finish the page
pdfDocument.finishPage(page);
Save pdf to file
File pdfFile = new File(mFilePath); // Write PDFDocument to the file. FileOutputStream fos = new FileOutputStream(pdfFile); pdfDocument.writeTo(fos); // Close output stream fos.close(); // close the document pdfDocument.close();
Above code will create a simple PDF with one page that contains TextView inside it.
Now let's start creating an actual PDF...
Creating PDF: Simple
PdfDocument API will renders any views to PDF. It is as simple as creating an activity, getting its view
and rendering PdfDocument of that view.
A Simple PDF page is of
height="842px"
andwidth="595px"
Note: As dp and sp are device dependent, make sure you create all of your layout in px as it will look same in all devices.
Steps:
- Create an
Activity
which will have a verticalLinearLayout
withheight="842px"
andwidth="595px"
. - Add all views with static data inside it.
- Make sure no view exceeds allotted height as it will be trimmed.
- If you want to create new page just repeat 1-3.
- In your
onCreate()
, get reference of allLinearLayout
and create PDF (using step 4) with each layout as one page. - Done.
P.S. in next chapter we will create more advanced PDF and dynamically insert data and create multi page PDF
To make your work easier i have created a Library to generate dynamic PDF using above methods View on Github