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 = "";
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 = FileDataProvider(new File(saveLocation)));