1

I have html file with form and canvas that need to filling by someone(include the draw on the canvas).
How to convert the html with the content after filling form to PDF file and save the PDF on the device?
I googled, but didn't find any good solution.

Update: All the html,css, js files are in assest folder.

tal
  • 295
  • 1
  • 4
  • 20

2 Answers2

1

You can use iText

First you need to add dependency-:

dependencies {
compile 'com.itextpdf:itextg:5.5.11'
}

Sample code

private void createPDF (String pdfFilename){

  //path for the PDF file to be generated
  String path = "docs/" + pdfname;
  PdfWriter pdfWriter = null;

  //create a new document
  Document document = new Document();

  try {

   //get Instance of the PDFWriter
   pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(path));

   //document header attributes
   document.addAuthor("betterThanZero");
   document.addCreationDate();
   document.addProducer();
   document.addCreator("MySampleCode.com");
   document.addTitle("Demo for iText XMLWorker");
   document.setPageSize(PageSize.LETTER);

   //open document
   document.open();

   //To convert a HTML file from the filesystem
   //String File_To_Convert = "docs/SamplePDF.html";
   //FileInputStream fis = new FileInputStream(File_To_Convert);

   //URL for HTML page
   URL myWebPage = new URL("http://demo.mysamplecode.com/");
   InputStreamReader fis = new InputStreamReader(myWebPage.openStream());

   //get the XMLWorkerHelper Instance
   XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
   //convert to PDF
   worker.parseXHtml(pdfWriter, document, fis);

   //close the document
   document.close();
   //close the writer
   pdfWriter.close();

  }   

  catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (DocumentException e) {
   e.printStackTrace();
  }       

 }

EDIT

Below code is working for me to get html file from assests folder -:

InputStream is = getAssets().open("test.html");
InputStreamReader fis =  new InputStreamReader(is);

You can change

URL myWebPage = new URL("http://demo.mysamplecode.com/");
InputStreamReader fis = new InputStreamReader(myWebPage.openStream());

to

InputStream is = getAssets().open("test.html");
InputStreamReader fis =  new InputStreamReader(is);

May, Help you. Happy Coding :)

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
  • There is a problem with your dependencies. android studio show me an error message: "Failed to resolve: com.itextpdf:itextg:5.5.11" i try to search jar file but it also same problem. you have idea? – tal Oct 17 '17 at 13:25
  • try to use compile 'com.itextpdf:itextg:5.5.10' this one is working for me – UltimateDevil Oct 17 '17 at 13:31
  • I have worked with this library such a cool library for workng with pdf – UltimateDevil Oct 17 '17 at 13:32
  • What exception you need read and write permission in manifest file as well as need runtime permission if you are working above marshmallow @tal – UltimateDevil Oct 18 '17 at 10:32
  • android studio throw me FileNotFoundException. See my update question – tal Oct 18 '17 at 10:41
  • try this link to open html https://stackoverflow.com/a/29939389/7073808 – UltimateDevil Oct 18 '17 at 11:04
  • I try this link before.. I get the error message:/android_asset/html/testForm.html: open failed: ENOENT (No such file or directory) – tal Oct 18 '17 at 11:42
  • Sorry but i think you don't understand me, I edit my question – tal Oct 19 '17 at 05:07
  • Have your problem solved....as you mentioned in your comment...you are getting no such file or directory...then I updated my answer... – UltimateDevil Oct 21 '17 at 02:07
1

https://github.com/blink22/react-native-html-to-pdf/blob/master/android/src/main/java/android/print/PdfConverter.java

PdfConverter converter = PdfConverter.getInstance();
File file = new File(Environment.getExternalStorageDirectory().toString(), "file.pdf");
String htmlString = "html code here";
converter.convert(getContext(), htmlString, file);
Armali
  • 18,255
  • 14
  • 57
  • 171