[FunctionName("PrintPdf")]public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context){ log.LogInformation("Entered PrintPdf API function..."); // Apply license keyIronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"; // Configure loggingIronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;IronPdf.Logging.Logger.CustomLogger = log;IronPdf.Logging.Logger.EnableDebugging = false; // Configure IronPdf settingsInstallation.LinuxAndDockerDependenciesAutoConfig = false;Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled; try { log.LogInformation("About to render pdf..."); // Create a renderer and render the URL as PDF ChromePdfRenderer renderer = new ChromePdfRenderer(); var pdf = renderer.RenderUrlAsPdf("https://www.google.com/"); log.LogInformation("Finished rendering pdf..."); // Return the rendered PDF as a file download return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" }; } catch (Exception e) { log.LogError(e, "Error while rendering pdf"); } return new OkObjectResult("OK");}
[FunctionName("PrintPdf")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log, ExecutionContext context)
{
log.LogInformation("Entered PrintPdf API function...");
// Apply license key
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
// Configure logging
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
IronPdf.Logging.Logger.CustomLogger = log;
IronPdf.Logging.Logger.EnableDebugging = false;
// Configure IronPdf settings
Installation.LinuxAndDockerDependenciesAutoConfig = false;
Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
try
{
log.LogInformation("About to render pdf...");
// Create a renderer and render the URL as PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");
log.LogInformation("Finished rendering pdf...");
// Return the rendered PDF as a file download
return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
}
catch (Exception e)
{
log.LogError(e, "Error while rendering pdf");
}
return new OkObjectResult("OK");
}
<FunctionName("PrintPdf")>PublicSharedAsync Function Run( <HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route:=Nothing)> req AsHttpRequest, log AsILogger, context AsExecutionContext) AsTask(OfIActionResult) log.LogInformation("Entered PrintPdf API function...") ' Apply license keyIronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" ' Configure loggingIronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.CustomIronPdf.Logging.Logger.CustomLogger = logIronPdf.Logging.Logger.EnableDebugging = False ' Configure IronPdf settingsInstallation.LinuxAndDockerDependenciesAutoConfig = FalseInstallation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.DisabledTry log.LogInformation("About to render pdf...") ' Create a renderer and render the URL as PDF Dim renderer As New ChromePdfRenderer() Dim pdf = renderer.RenderUrlAsPdf("https://www.google.com/") log.LogInformation("Finished rendering pdf...") ' Return the rendered PDF as a file download Return New FileContentResult(pdf.BinaryData, "application/pdf") With {.FileDownloadName = "google.pdf"}Catch e AsException log.LogError(e, "Error while rendering pdf")EndTry Return New OkObjectResult("OK")End Function
<FunctionName("PrintPdf")>
Public Shared Async Function Run(
<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route:=Nothing)> req As HttpRequest,
log As ILogger, context As ExecutionContext) As Task(Of IActionResult)
log.LogInformation("Entered PrintPdf API function...")
' Apply license key
IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
' Configure logging
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom
IronPdf.Logging.Logger.CustomLogger = log
IronPdf.Logging.Logger.EnableDebugging = False
' Configure IronPdf settings
Installation.LinuxAndDockerDependenciesAutoConfig = False
Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
Try
log.LogInformation("About to render pdf...")
' Create a renderer and render the URL as PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.google.com/")
log.LogInformation("Finished rendering pdf...")
' Return the rendered PDF as a file download
Return New FileContentResult(pdf.BinaryData, "application/pdf") With {.FileDownloadName = "google.pdf"}
Catch e As Exception
log.LogError(e, "Error while rendering pdf")
End Try
Return New OkObjectResult("OK")
End Function
高度な HTML 文字列レンダリングの例
CSSスタイリングによるカスタムHTMLを含む、より複雑なシナリオについては、HTML String to PDF機能を使用することができます:
[FunctionName("RenderHtmlWithCss")]public static async Task<IActionResult> RenderHtmlWithCss( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log){ log.LogInformation("Processing HTML to PDF request"); // Read HTML content from request body string htmlContent = await new StreamReader(req.Body).ReadToEndAsync(); // Configure renderer with custom options var renderer = new ChromePdfRenderer() {RenderingOptions = new ChromePdfRenderOptions() {MarginTop = 20,MarginBottom = 20,MarginLeft = 10,MarginRight = 10,CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print,PrintHtmlBackgrounds = true,CreatePdfFormsFromHtml = true } }; try { // Add custom CSS string styledHtml = $@" <html> <head> <style> body {{font-family: Arial, sans-serif; padding: 20px;}} h1 {{color: #2c3e50;}} .highlight {{background-color: #f1c40f; padding: 5px;}} </style> </head> <body> {htmlContent} </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(styledHtml); return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "styled-document.pdf" }; } catch (Exception ex) { log.LogError(ex, "Failed to render HTML to PDF"); return new BadRequestObjectResult("Error processing HTML content"); }}
[FunctionName("RenderHtmlWithCss")]
public static async Task<IActionResult> RenderHtmlWithCss(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing HTML to PDF request");
// Read HTML content from request body
string htmlContent = await new StreamReader(req.Body).ReadToEndAsync();
// Configure renderer with custom options
var renderer = new ChromePdfRenderer()
{
RenderingOptions = new ChromePdfRenderOptions()
{
MarginTop = 20,
MarginBottom = 20,
MarginLeft = 10,
MarginRight = 10,
CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
}
};
try
{
// Add custom CSS
string styledHtml = $@"
<html>
<head>
<style>
body {{font-family: Arial, sans-serif; padding: 20px;}}
h1 {{color: #2c3e50;}}
.highlight {{background-color: #f1c40f; padding: 5px;}}
</style>
</head>
<body>
{htmlContent}
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(styledHtml);
return new FileContentResult(pdf.BinaryData, "application/pdf")
{
FileDownloadName = "styled-document.pdf"
};
}
catch (Exception ex)
{
log.LogError(ex, "Failed to render HTML to PDF");
return new BadRequestObjectResult("Error processing HTML content");
}
}
Azureの無料および共有ティアと消費プランは、PDFレンダリングには適していません。 私たち自身が使用しているAzure B1ホスティング/プレミアムプランをお勧めします。 HTML to PDFのプロセスは、コンピューターにとって多くの"作業"を要するものであり、自分のマシンでウェブページを開いてレンダリングすることに似ています。実際のブラウザエンジンが使用されるため、適切に用意し、同等のパワーのデスクトップマシンに似たレンダリング時間を期待する必要があります。
What are the best practices for using IronPDF in production on Azure?
Best practices include using appropriate hosting tiers, configuring logging with Azure Application Insights, handling exceptions, optimizing HTML content, and rigorous testing before deployment.
When should I consider using Docker with IronPDF on Azure?
Consider using Docker for better control over the runtime environment and to eliminate platform-specific limitations, especially for SVG font access or performance control.
How can I access Azure logs for troubleshooting IronPDF operations?
Refer to our Azure Log Files guide for instructions on accessing and interpreting logs to troubleshoot PDF generation issues specific to IronPDF operations.