Converting HTML Files to Pdf

converting an HTML page to a PDF

So the answer to this ended up being a combination of a few things.

1) I had to edit the CSS to make sure images were actually staying within the bounds of the "100%" of the page. I added CSS to make sure all images maxed out at 90%. This prevented any side-to-side scrolling on the page and prevented anything getting cut off in the PDF.

2) I went into the Page Setup section off of the File menu in the browser, went to the "Margins and Footers" tab and there were the places that were inscribing things like date/url/page number. I blanked all those out and all was good.

3) The trickiest part was making sure it split pages in the correct places. I finally learned about the CSS properties called page-break-before and page-break-inside. I created classes with each of these set like so:

// Creates a page break in the PDF right before element with this class
.pagebreak{
page-break-before: always;
}

// Doesn't allow the contents of a div with this class to split across pages.
.nobreak{
page-break-inside: avoid;
}

and, when converted to PDF, it read these and laid out the pages as I wanted. It was a bit tedious, but I ended up with the exact PDF that I wanted!

How to convert Html page to Pdf in android studio

Try this code :

PdfConverter converter = PdfConverter.getInstance();
String directory_path = Environment.getExternalStorageDirectory().toString();
File file = new File(directionPath, "name_file.pdf");
String htmlString = "<html><body><p>Hello World</p></body></html>";
converter.convert(getContext(), htmlString, file);

click
here to add file PdfConverter

Converting HTML files to PDF

The Flying Saucer XHTML renderer project has support for outputting XHTML to PDF. Have a look at an example here.



Related Topics



Leave a reply



Submit