Rendering Settings for HTML to PDF

import com.ironsoftware.ironpdf.*;  
import com.ironsoftware.ironpdf.render.*;
import java.io.IOException;
import java.nio.file.Paths;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Create a new ChromePdfRenderOptions object
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();

// Set custom options for the generated PDF document (paper size, title, etc.)
renderOptions.setCustomPaperSizeInCentimeters(31.75, 50.80 );
renderOptions.setPrintHtmlBackgrounds(true);
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
renderOptions.setTitle("My PDF Document Name");
renderOptions.setEnableJavaScript(true);
renderOptions.setWaitFor(new WaitFor(50)); // time in milliseconds
renderOptions.setCssMediaType(CssMediaType.SCREEN);
renderOptions.UseScaledRendering(100); // 100 is zoom percentage
renderOptions.setCreatePdfFormsFromHtml(true);

// Customize PDF Margins by specifying sizes in millimeters
renderOptions.setMarginTop(40);
renderOptions.setMarginBottom(40);
renderOptions.setMarginLeft(20);
renderOptions.setMarginRight(20);

// Render the HTML document as a PDF using the specified rendering options.
try {
    PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf("wikipedia.html", renderOptions);
    pdf.saveAs(Paths.get("assets/wikipedia.pdf"));
} catch (IOException e) {
    e.printStackTrace();
}
mvn install
Rendering Settings for HTML to PDF

Rendering Settings for HTML to PDF

此程式碼範例展示了IronPDF在將HTML內容轉換為PDF文件時為開發者提供的強大功能和靈活性。

ChromePdfRenderOptions確保開發者對如何將網頁轉換為PDF具有完全控制。

使用ChromePdfRenderOptions類別,開發者可以自訂頁面邊距、指定頁面方向、新增註釋、包括書籤、更改文件中繼資料、調整縮放比例、修改CSS規則集等等。 一旦定義好,可以將該類別的物件作為第二個(可選)參數傳入任何靜態PDF文件渲染方法(renderUrlAsPdf),以改變IronPDF解析網站內容及在這些方法執行過程中渲染PDF內容的方式。

在API參考中探索ChromePdfRenderOptions自定義,了解API參考中可用自定義的詳細列表。

了解如何使用Java和IronPDF建立PDF文件
在 GitHub 查看

準備好開始了嗎?

版本:2026.10剛剛發布