I have to following sample code which creates a print preview from WebView.
public class PrintLauncherFragmentActivity extends AppCompatActivity {
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// https://developer.android.com/training/printing/html-docs
// Create a WebView object specifically for printing
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
createWebPrintJob(view);
mWebView = null;
}
});
String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " +
"testing, testing...</p></body></html>";
webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);
// Keep a reference to WebView object until you pass the PrintDocumentAdapter
// to the PrintManager
mWebView = webView;
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
String jobName = "demo";
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);
// Create a print job with name and adapter instance
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
// Save the job object for later status checking
//printJobs.add(printJob);
}
}
Things look good on print preview page
However, when I try to save a PDF file to Download using the top right button in print preview page, it always produce a 0 size invalid PDF file.
Does anyone know why and how I can avoid such?
Note, I have already allowed Files and media permission
from Saving PDF from webview print function always produce a 0 size invalid PDF file



No comments:
Post a Comment