Open PDFs from URLs in Java

This guide explains how to open a PDF document from a remote URL by saving to a local file.

Download the file using the Java URL.openStream function and save it to a location on the file system:

String url = "https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf";
String saveLocation = "~/Downloads/download.pdf";
InputStream in = new URL(url).openStream();
Files.copy(in, Paths.get(saveLocation), StandardCopyOption.REPLACE_EXISTING);

Now open the file as you would normally using a FileDataProvider:

PdfDocument document = PdfDocument.open(new FileDataProvider(new File(saveLocation)));