3

I have a form that when currently filled out is sent to a dedicated email address on submission. I would like to have the opportunity to offer other options. Like saving it as a PDF file. The form is completed in a main.xml layout. Is it possible to turn xml layouts into PDF's? If so could someone point me in the direction of a good example.

Many Thanks

PaulH
  • 263
  • 2
  • 7
  • 18
  • There should be tutorials on google. – alicanbatur May 10 '13 at 09:42
  • The only ones I can seem to find are ones that create from the activity rather than the layout. – PaulH May 10 '13 at 09:44
  • i don't know about this but have you gone through this link http://stackoverflow.com/questions/7806847/converting-xml-to-pdf-using-styles-from-xsl it might help you and also http://stackoverflow.com/questions/212577/how-do-you-create-a-pdf-from-xml-in-java?rq=1 – surhidamatya May 10 '13 at 09:45
  • again i found this http://stackoverflow.com/questions/15003337/xml-to-pdf-form?rq=1 – surhidamatya May 10 '13 at 09:52
  • Thanks sur007. Reading the third link it would seem as if there isnt a quick click to convert available for xml-pdf. – PaulH May 10 '13 at 10:13
  • sur007 do you know if this would be a similar thing for a xml to csv? – PaulH May 10 '13 at 14:20

2 Answers2

4

You can take a look on this link:

https://github.com/HendrixString/Android-PdfMyXml

Instructions

  1. create XML layouts

First create XML layouts. give it dimensions in pixels (and for all it's sub views) and proportions according landscape or portrait according to ratio 1:1.41.

page1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="2115px"
            android:layout_height="1500px"
            android:background="@color/white">
 <TextView android:id="@+id/tv_hello"
            android:textColor="@color/black"
            android:textSize="27px"
            android:textStyle="bold"
            android:padding="6px"/>

</RelativeLayout>

you can create as many as pages/templates as you need.

  1. Implement a View renderer

implement your View renderer by extending AbstractViewRenderer or by anonymously instantiating it and injecting the layout id. the initView(View view) will supply you an inflated View automatically. There are other options but I wont cover it now.

AbstractViewRenderer page = new AbstractViewRenderer(context, R.layout.page1) {
private String _text;

public void setText(String text) {
    _text = text;
}

@Override
protected void initView(View view) {
    TextView tv_hello = (TextView)view.findViewById(R.id.tv_hello);
     tv_hello.setText(_text);
}
};

// you can reuse the bitmap if you want
page.setReuseBitmap(true);
  1. Build the PDF document

Use PdfDocument or PdfDocument.Builder to add pages and render and run it all at background with progress bar.

    PdfDocument doc            = new PdfDocument(ctx);

// add as many pages as you have
doc.addPage(page);

doc.setRenderWidth(2115);
doc.setRenderHeight(1500);
doc.setOrientation(PdfDocument.A4_MODE.LANDSCAPE);
doc.setProgressTitle(R.string.gen_please_wait);
doc.setProgressMessage(R.string.gen_pdf_file);
doc.setFileName("test");
doc.setInflateOnMainThread(false);
doc.setListener(new PdfDocument.Callback() {
    @Override
    public void onComplete(File file) {
        Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");
    }

    @Override
    public void onError(Exception e) {
        Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
    }
});

doc.createPdf(ctx);

or use PdfDocument.Builder

new PdfDocument.Builder(ctx).addPage(page).filename("test").orientation(PdfDocument.A4_MODE.LANDSCAPE)
                         .progressMessage(R.string.gen_pdf_file).progressTitle(R.string.gen_please_wait).renderWidth(2115).renderHeight(1500)
                         .listener(new PdfDocument.Callback() {
                             @Override
                             public void onComplete(File file) {
                                 Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");
                             }

                             @Override
                             public void onError(Exception e) {
                                 Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
                             }
                         }).create().createPdf(this);
2

You can use this library to make it easy to do within a few lines of code -

 PdfGenerator.getBuilder()
                        .setContext(context)
                        .fromLayoutXMLSource()
                        .fromLayoutXML(R.layout.layout_print,R.layout.layout_print)
                        .s̶e̶t̶D̶e̶f̶a̶u̶l̶t̶P̶a̶g̶e̶S̶i̶z̶e̶(̶P̶d̶f̶G̶e̶n̶e̶r̶a̶t̶o̶r̶.P̶a̶g̶e̶S̶i̶z̶e̶.A̶4̶)̶ //No Longer Work
                        .setFileName("Test-PDF")
                        .build();

Additionally you can also pass a callback (PdfGeneratorListener()) in .build() to notify about if the pdf generation is done or failed for an exception

Suraj Sahani
  • 15
  • 1
  • 10
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42