Skip to content

Instantly share code, notes, and snippets.

@brettwold
Last active October 11, 2019 08:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettwold/e8dc5ff02ecb07aebf4c96e130caf65b to your computer and use it in GitHub Desktop.
Save brettwold/e8dc5ff02ecb07aebf4c96e130caf65b to your computer and use it in GitHub Desktop.
public class PdfPrintUsage extends Activity {
private WebView wv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
wv = (WebView) findViewById(R.id.web_view);
}
private void createWebPrintJob(WebView webView) {
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
.setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/PDFTest/");
PdfPrint pdfPrint = new PdfPrint(attributes);
pdfPrint.print(webView.createPrintDocumentAdapter(jobName), path, "output_" + System.currentTimeMillis() + ".pdf");
}
}
@bncomputer3
Copy link

how i call this button click event? That means click on download button then download a pdf .

@KsgFellow
Copy link

Great guide, nice code snippet.
Would there be a possibility to get the pdf as outputstream instead of as a file on the device storage?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment