HTML到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
HTML到PDF的渲染设置

HTML到PDF的渲染设置

这个代码示例展示了IronPDF为开发人员在将HTML内容转换为PDF文档中提供的强大功能和灵活性。

ChromePdfRenderOptions确保开发人员对网页如何转换为PDF具有完全控制。

使用ChromePdfRenderOptions类,开发人员可以自定义页面边距、指定页面方向、添加注释、包括书签、改变文档元数据、调整缩放比例、修改CSS规则集等。 一旦定义,这类对象可作为第二(可选)参数提供给任何静态PDF文档渲染方法(renderHtmlAsPdf, renderHtmlFileAsPdf, 和renderUrlAsPdf)来改变IronPDF对网站内容的解释,并在这些方法执行期间渲染PDF内容的方式。

在API参考中探索ChromePdfRenderOptions自定义选项,以获取API参考中可用自定义选项的详细列表。

了解如何使用 IronPDF for Java 创建 PDFs
在 GitHub 上查看

准备开始了吗?

版本:2026.10刚刚发布