IronPDF が HTML 文字列を PDF にレンダリングする RenderHtmlAsPdf メソッドの例です。 パラメータは、PDFにレンダリングされるHTML文字列です。 このメソッドは ChromePdfRenderer クラスの一部であり、PDF 生成のニーズに合わせて レンダリングオプション を広範に制御できます。
using IronPdf;// Instantiate Renderervar renderer = new ChromePdfRenderer();// Create a PDF from a HTML string using C#var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");// Export to a file or Streampdf.SaveAs("output.pdf");
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
ImportsIronPdf' Instantiate RendererPrivate renderer = New ChromePdfRenderer()' Create a PDF from a HTML string using C#Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")' Export to a file or Streampdf.SaveAs("output.pdf")
Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Create a PDF from a HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
// Example with inline CSSvar styledHtml = @"<html><head> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: #333; border-bottom: 2px solid #0066cc; } p { line-height: 1.6; } </style></head><body> <h1>Professional Report</h1> <p>This PDF was generated from HTML with custom styling.</p></body></html>";using var styledPdf = renderer.RenderHtmlAsPdf(styledHtml);styledPdf.SaveAs("styled-output.pdf");
// Example with inline CSS
var styledHtml = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; border-bottom: 2px solid #0066cc; }
p { line-height: 1.6; }
</style>
</head>
<body>
<h1>Professional Report</h1>
<p>This PDF was generated from HTML with custom styling.</p>
</body>
</html>";
using var styledPdf = renderer.RenderHtmlAsPdf(styledHtml);
styledPdf.SaveAs("styled-output.pdf");
Dim styledHtml AsString = "<html><head> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: #333; border-bottom: 2px solid #0066cc; } p { line-height: 1.6; } </style></head><body> <h1>Professional Report</h1> <p>This PDF was generated from HTML with custom styling.</p></body></html>"Using styledPdf = renderer.RenderHtmlAsPdf(styledHtml) styledPdf.SaveAs("styled-output.pdf")EndUsing
Dim styledHtml As String = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; border-bottom: 2px solid #0066cc; }
p { line-height: 1.6; }
</style>
</head>
<body>
<h1>Professional Report</h1>
<p>This PDF was generated from HTML with custom styling.</p>
</body>
</html>"
Using styledPdf = renderer.RenderHtmlAsPdf(styledHtml)
styledPdf.SaveAs("styled-output.pdf")
End Using
using IronPdf;// Instantiate Renderervar renderer = new ChromePdfRenderer();// Advanced Example with HTML Assets// Load external html assets: Images, CSS and JavaScript.// An optional BasePath 'C:\site\assets\' is set as the file location to load assets fromvar myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");myAdvancedPdf.SaveAs("html-with-assets.pdf");
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
ImportsIronPdf' Instantiate RendererPrivate renderer = New ChromePdfRenderer()' Advanced Example with HTML Assets' Load external html assets: Images, CSS and JavaScript.' An optional BasePath 'C:\site\assets\' is set as the file location to load assets fromPrivate myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")myAdvancedPdf.SaveAs("html-with-assets.pdf")
Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Advanced Example with HTML Assets
' Load external html assets: Images, CSS and JavaScript.
' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
Private myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
// Using a web URL as base pathvar webBasedPdf = renderer.RenderHtmlAsPdf( "<link rel='stylesheet' href='/styles/main.css'><h1>Web Content</h1>", "https://mywebsite.com");
// Using a web URL as base path
var webBasedPdf = renderer.RenderHtmlAsPdf(
"<link rel='stylesheet' href='/styles/main.css'><h1>Web Content</h1>",
"https://mywebsite.com"
);
' Using a web URL as base pathDim webBasedPdf = renderer.RenderHtmlAsPdf( "<link rel='stylesheet' href='/styles/main.css'><h1>Web Content</h1>", "https://mywebsite.com")
' Using a web URL as base path
Dim webBasedPdf = renderer.RenderHtmlAsPdf(
"<link rel='stylesheet' href='/styles/main.css'><h1>Web Content</h1>",
"https://mywebsite.com"
)
How can I ensure my external web resources are correctly referenced in IronPDF?
To ensure external web resources are correctly referenced, specify the correct `BasePath` or use a full URL as the base path for web-based content. This includes images, stylesheets, and scripts.
What is the role of `ChromePdfRenderer` in IronPDF?
The `ChromePdfRenderer` in IronPDF is responsible for converting HTML to PDF. It uses a fully functional version of the Google Chromium engine to ensure accurate rendering as seen in a web browser.