Konvèti URL nan PDF nan Java

Gid sa a ap montre ou kijan pou konvèti URL nan PDF nan Java. Li gen tout detay sou kòman pou konfigire IDE a, yon lis etap ki dekri pwosesis pou konvèti URL nan PDF nan Java ak yon egzanp kòd pou devlope yon konvètisè debaz. Genyen anpil opsyon pou personnaliser PDF sòti a selon bezwen ou.

Etap pou Chanje Lyen an PDF Dokiman nan Java

  1. Configure IDE a pou itilize Aspose.PDF pou Java pou konvèti URL nan PDF.
  2. Defini URL paj wèb la pou konvèti nan PDF.
  3. Konfigire anviwònman paj PDF sòti a ak lòt paramèt ak klas HtmlLoadOptions.
  4. Chaje kontni paj wèb la.
  5. Jenere yon dosye PDF soti nan kontni paj wèb la pa chaje li nan objè Document.
  6. Sove dokiman PDF ki sòti a.

Sa yo se etap yo ki montre kijan pou tounen URL nan PDF nan Java. Defini URL paj wèb la, konfigire anviwònman paj PDF sòti a, epi chaje kontni paj wèb la. Kreye yon objè PDF Document soti nan kontni paj wèb la epi sove PDF sòti a ak anviwònman paj yo ou vle.

Kòd pou Konvèti Paj Sit Wèb nan PDF nan 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();
}
}

Kòd sa a montre devlopman yon URL lyen nan PDF konvètisè nan Java. Ou ka mete margin paj la, eta tèks default la, yon flag pou rann tout kontni an sou yon sèl paj, ak anviwònman layout paj la. Si ou deja telechaje kontni paj wèb la, ou ka konvèti li nan PDF pa kreye yon objè dokiman epi sove li sou disk.

Nan atik sa a, nou te aprann kijan pou konvèti URL nan PDF. Si ou vle ekstrè lyen nan PDF, gade atik sou Ekstrè lyen nan PDF nan Java.

 Հայերեն