Azure上で.NETを使用してHTMLからPDFを実行する方法は?

This article was translated from English: Does it need improvement?
Translated
View the article in English

はい。 IronPDFは、Azure上でPDFドキュメントを生成、操作、読み取るために使用できます。 IronPDFは、多くのAzureプラットフォーム上で、MVCウェブサイトやAzure Functionsなどを含めて徹底的にテストされています。

Docker 上の Azure Functions

Docker コンテナ内で Azure Functions を実行している場合は、以下をご参照ください。 このチュートリアル その代わりに


チュートリアルの方法

プロジェクトの設定

IronPDFをインストールして始めましょう

最初のステップは、NuGetを使用してIronPDFをインストールすることです。

Install-Package IronPdf

または、.dll ファイルを手動でインストールするには、 直接ダウンロード リンク.

正しいAzureオプションを選択

Azureレベルのホスティングレベルを選択する

Azure Basic B1 は、エンドユーザーのレンダリングニーズに必要な最低ホスティングレベルです。 高スループットシステムを作成している場合、これをアップグレードする必要があるかもしれません。

続行する前に
App service planのプランタイプを選択しないと、IronPDFがPDF文書のレンダリングに失敗する可能性があります。

Azureレベルのホスティングレベルを選択する

「パッケージファイルから実行」チェックボックス

Azure Functions アプリケーションを公開する際は、「Run from package file」が選択されていないことを確認してください。

.NET 6 の構成

マイクロソフトは最近、イメージングライブラリを.NET 6+から削除し、多くのレガシーAPIを破壊しました。そのため、これらのレガシーAPI呼び出しを引き続き許可するようにプロジェクトを設定する必要があります。

  1. Linuxでは、マシンにlibgdiplusをインストールするために、Installation.LinuxAndDockerDependenciesAutoConfig=true;を設定してください。

  2. 以下を .NET 6 プロジェクトの .csproj ファイルに追加してください:Trueもちろん、英語のテキストを教えていただけますでしょうか?

  3. プロジェクト内に runtimeconfig.template.json というファイルを作成し、次の内容を記入してください:
{
      "configProperties": {
         "System.Drawing.EnableUnixSupport": true
      }
}
{
      "configProperties": {
         "System.Drawing.EnableUnixSupport": true
      }
}
If True Then
	  "configProperties":
	  If True Then
		 "System.Drawing.EnableUnixSupport": True
	  End If
End If
VB   C#
  1. 最後に、プログラムの先頭に次の行を追加します: System.AppContext.SetSwitch(「System.Drawing.EnableUnixSupport」、true);

AzureでDockerを使用する

Azureでパフォーマンスを制御し、SVGフォントへのアクセスを得る一つの方法として、Dockerコンテナ内からIronPDFアプリケーションとFunctionsを使用することが挙げられます。

私たちは包括的な IronPDF Azure Docker チュートリアル LinuxおよびWindowsインスタンス向けのものであり、読むことが推奨されています。

Azure Function コード例

この例では、ログエントリを自動的に組み込みのAzureロガーに出力します。 (ILogger logを参照してください):

[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";
    // Enable log
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;

    IronPdf.Logging.Logger.EnableDebugging = false;
    // Configure IronPdf
    Installation.LinuxAndDockerDependenciesAutoConfig = false;
    Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    try
    {
        log.LogInformation("About to render pdf...");
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        // Render PDF
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");
        log.LogInformation("finished rendering pdf...");
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering pdf", e);
    }

    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";
    // Enable log
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom;
    IronPdf.Logging.Logger.CustomLogger = log;

    IronPdf.Logging.Logger.EnableDebugging = false;
    // Configure IronPdf
    Installation.LinuxAndDockerDependenciesAutoConfig = false;
    Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    try
    {
        log.LogInformation("About to render pdf...");
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        // Render PDF
        var pdf = renderer.RenderUrlAsPdf("https://www.google.com/");
        log.LogInformation("finished rendering pdf...");
        return new FileContentResult(pdf.BinaryData, "application/pdf") { FileDownloadName = "google.pdf" };
    }
    catch (Exception e)
    {
        log.LogError(e, "Error while rendering pdf", e);
    }

    return new OkObjectResult("OK");
}
<FunctionName("PrintPdf")>
Public Shared Async Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> ByVal req As HttpRequest, ByVal log As ILogger, ByVal context As ExecutionContext) As Task(Of IActionResult)
	log.LogInformation("Entered PrintPdf API function...")
	' Apply license key
	IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
	' Enable log
	IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.Custom
	IronPdf.Logging.Logger.CustomLogger = log

	IronPdf.Logging.Logger.EnableDebugging = False
	' Configure IronPdf
	Installation.LinuxAndDockerDependenciesAutoConfig = False
	Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
	Try
		log.LogInformation("About to render pdf...")
		Dim renderer As New ChromePdfRenderer()
		' Render PDF
		Dim pdf = renderer.RenderUrlAsPdf("https://www.google.com/")
		log.LogInformation("finished rendering pdf...")
		Return New FileContentResult(pdf.BinaryData, "application/pdf") With {.FileDownloadName = "google.pdf"}
	Catch e As Exception
		log.LogError(e, "Error while rendering pdf", e)
	End Try

	Return New OkObjectResult("OK")
End Function
VB   C#

既知の問題

共有ホスティングプランではSVGフォントのレンダリングは利用できません。

見つかった制限の一つは Azureホスティングプラットフォーム 安価な共有ウェブアプリティアでは、サーバーがGoogleフォントなどのSVGフォントを読み込むことをサポートしていません。 これは、これらの共有ホスティングプラットフォームがセキュリティ上の理由でWindowsのGDI+グラフィックオブジェクトにアクセスすることが許可されていないためです。

以下の内容を日本語に翻訳してください:

使用をお勧めします WindowsまたはLinux Dockerコンテナ この問題を解決するために、最適なフォントレンダリングが必要な場合、Azure上のVPSを利用するのも一つの方法です。

Azureの無料ティアホスティングは遅いです。

Azureの無料および共有レベル、または従量課金プランは、PDFレンダリングには適していません。 私たちは、自分たち自身で使用しているAzure B1ホスティング/プレミアムプランを推奨します。 HTML から PDF への変換プロセスは、あらゆるコンピューターにとって重要な「仕事」です。これは、あなた自身のマシンでウェブページを開いてレンダリングすることに似ています。実際のブラウザエンジンが使用されるため、それに応じてプロビジョニングする必要があり、同様の性能を持つデスクトップマシンのレンダリング時間と同程度の時間がかかると予想されます。

技術サポートリクエストチケットの作成

要求チケットを作成するには、「IronPDFのエンジニアリングサポートリクエストの作成方法' ガイド