Changes

Jump to: navigation, search

An Android 6 HTML and Web Content Printing Example

3,743 bytes added, 21:57, 5 January 2016
Printing Dynamic HTML Content
String htmlDocument =
"<html><body><h1>Android Print Test</h1><p>"
+ "This is some sample content.</p></body></html>";
 
webView.loadDataWithBaseURL(null, htmlDocument,
"text/HTML", "UTF-8", null);
 
myWebView = webView;
}
.
.
.
}
</pre>
 
The code changes begin by declaring a variable named myWebView in which will be stored a reference to the WebView instance used for the printing operation. Within the onCreate() method, an instance of the WebView class is created to which a WebViewClient instance is then assigned.
 
The WebViewClient assigned to the web view object is configured to indicate that loading of the HTML content is to be handled by the WebView instance (by returning false from the shouldOverrideUrlLoading() method). More importantly, an onPageFinished() handler method is declared and implemented to call a method named createWebPrintJob(). The onPageFinished() method will be called automatically when all of the HTML content has been loaded into the web view. As outlined in the previous chapter, this step is necessary when printing dynamically created HTML content to ensure that the print job is not started until the content has fully loaded into the WebView.
 
Next, a String object is created containing some HTML to serve as the content and subsequently loaded into the web view. Once the HTML is loaded, the onPageFinished() callback method will trigger. Finally, the method stores a reference to the web view object in the previously declared myWebView variable. Without this vital step, there is a significant risk that the Java runtime system will assume that the application no longer needs the web view object and will discard it to free up memory resulting in the print job terminating before completion.
 
All that remains in this example is to implement the createWebPrintJob() method which is currently configured to be called by the onPageFinished() callback method. Remaining within the HTMLPrintActivity.java file, therefore, implement this method so that it reads as follows:
 
<pre>
private void createWebPrintJob(WebView webView) {
 
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
 
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter("MyDocument");
 
String jobName = getString(R.string.app_name) + " Print Test";
 
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
</pre>
 
This method obtains a reference to the PrintManager service and instructs the web view instance to create a print adapter. A new string is created to store the name of the print job (in this case based on the name of the application and the word “Print Test”).
 
Finally, the print job is started by calling the print() method of the print manager, passing through the job name, print adapter and a set of default print attributes.
 
Compile and run the application on a device running Android 5.0 or later. Once launched, the standard Android printing page should appear as illustrated in Figure 59-1.
 
 
[[Image:image235.png]]
Figure 59-1
 
 
Print to a physical printer if you have one configured, save to Google Drive or, alternatively, select the option to save to a PDF file. Once the print job has been initiated, check the generated output on your chosen destination. Note that when using the Save to PDF option, the system will request a name and location for the PDF file. The Downloads folder makes a good option, the contents of which can be viewed by selecting the Downloads icon located amongst the other app icons on the device. Figure 59 2, for example, shows the PDF output generated by the Save to PDF option viewed on an Android device:
 
[[Image:image236.png]]
 
Figure 59-2
 
<h1>Android Print Test</h1><p>"
+ "This is some sample content.</p></body></html>";

Navigation menu