URL'yi PDF'ye Dönüştürme Java'da

Bu kısa eğitimde URL’yi PDF’ye nasıl dönüştürebileceğinizi gösteriyoruz. IDE’yi ayarlamak için tüm ayrıntılar, Java’da URL’yi PDF’ye dönüştürme işlemini açıklayan adımlar listesi ve temel bir dönüştürücü geliştirmek için örnek bir kodu içerir. Çıktı PDF’nizi gereksinimlerinize göre özelleştirebileceğiniz çeşitli seçenekler bulunmaktadır.

Java’da Bağlantıyı PDF Belgesine Dönüştürme Adımları

  1. URL’yi PDF’ye dönüştürmek için Aspose.PDF for Java kullanacak şekilde IDE’yi ayarlayın
  2. PDF’ye dönüştürmek için web sayfası URL’sini tanımlayın
  3. Çıktı PDF sayfası ayarlarını ve diğer parametreleri HtmlLoadOptions sınıfı ile yapılandırın
  4. Web sayfası içeriğini alın
  5. Web sayfası içeriğini Document nesnesine yükleyerek PDF dosyasını oluşturun
  6. Oluşan PDF belgesini kaydedin

Bu adımlar, Java’da URL’yi PDF’ye dönüştürme işlemini özetlemektedir. Web sayfası URL’sini tanımlayın, çıktı PDF sayfası ayarlarını yapılandırın ve web sayfası içeriğini alın. Web sayfası içeriğinden PDF Document nesnesi oluşturun ve istenilen sayfa ayarları ile çıktıyı kaydedin.

Java’da Web Sayfasını PDF’ye Dönüştürme Kodu

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();
}
}

Bu kod, URL bağlantısını PDF’ye dönüştürücü geliştirilmesini göstermektedir. Sayfa kenar boşluklarını, varsayılan metin durumunu, tüm içeriği tek bir sayfada göstermek için bayrağı ve sayfa düzenini ayarlayabilirsiniz. Web sayfası içeriğini zaten indirdiyseniz, belge nesnesi oluşturarak ve diske kaydederek bunu PDF’ye dönüştürebilirsiniz.

Bu makalede, URL’yi PDF’ye dönüştürmeyi öğrendik. PDF’den bağlantı çıkarmak isterseniz, Java’da PDF’den bağlantı çıkarmak başlıklı makaleye göz atabilirsiniz.

 Türkçe