Convert URL to PDF in Java

This short tutorial guides on how to convert URL to PDF in Java. It has all the details to set the IDE, a list of steps describing the process to transform URL to PDF in Java and a sample code for developing a basic converter. Various options are available for customization of the output PDF that you can perform according to your requirements.

  1. Set the IDE to use Aspose.PDF for Java to change the URL to PDF
  2. Define the webpage URL for conversion to PDF
  3. Configure the output PDF page settings and other parameters with the HtmlLoadOptions class
  4. Fetch the webpage content
  5. Generate a PDF file from the webpage contents by loading the contents into the Document object
  6. Save the resultant PDF document

These steps summarize how to turn URL into PDF in Java. Define the webpage URL, configure the output PDF page settings, and fetch the webpage contents. Create a PDF Document object from the webpage contents and save the output PDF using the desired page settings.

Code to Convert Website Page to PDF in Java

import com.aspose.pdf.*;
import java.io.*;
import java.net.*;
public class HtmlToPdfConverter {
public static void main(String[] args) throws Exception {
// Initialize and apply Aspose.PDF license
License pdfLicense = new License();
pdfLicense.setLicense("license.lic");
// Convert an online HTML page to PDF
generatePdfFromWebPage();
System.out.println("Webpage Link to PDF process finished.");
}
// Method to fetch and convert an HTML webpage to a PDF document
private static void generatePdfFromWebPage() {
// Define the webpage URL to be converted
final String webpageUrl = "https://docs.aspose.com/";
// Configure PDF page settings for conversion
HtmlLoadOptions pdfOptions = new HtmlLoadOptions(webpageUrl);
pdfOptions.getPageInfo().setWidth(1200); // Setting custom page width
pdfOptions.getPageInfo().setHeight(850); // Setting custom page height
pdfOptions.getPageInfo().setLandscape(false); // Keeping portrait orientation
// Fetch the webpage content and create a PDF document
try (InputStream webContentStream = fetchWebContentAsStream(webpageUrl);
Document pdfDocument = new Document(webContentStream, pdfOptions)) {
// Save the generated PDF file
pdfDocument.save("Converted_WebPage.pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to retrieve the content of a webpage as a stream
private static InputStream fetchWebContentAsStream(String webpageUrl) throws IOException {
// Create a URL object from the given webpage URL
URL url = new URL(webpageUrl);
// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set the request method to GET
connection.setRequestMethod("GET");
// Allow input stream retrieval
connection.setDoInput(true);
// Establish the connection
connection.connect();
// Return the webpage content as an input stream
return connection.getInputStream();
}
}

This code has demonstrated the development of a URL link to PDF converter in Java. You can set page margin, default text state, flag to render the whole content to a single page, and set page layout. If you have already downloaded the webpage contents, you may convert it to PDF by creating a document object and saving it on the disk.

IIn this article, we have learned to convert URLs to PDFs. If you want to extract links from PDF, refer to the article on Extract links from PDF in Java.

 English