PDFツール

C++でHTMLをPDFに変換する方法

更新済み 7月 1, 2023
共有:

HTMLファイルまたはコンテンツをPDFページ形式に変換する機能は、多くのアプリケーションにおいて貴重な機能です。 C++では、ゼロからアプリケーションを構築してHTMLをPDF形式のファイルに変換することは非常に手間のかかる作業です。 この記事では、wkhtmltopdfライブラリを使用してC++でHTMLをPDFに変換する方法を探ります。

WKHtmltoPdf

IronPDF allows developers to integrate HTML to PDF conversion into their .NET projects using the WKHtmltoPdf rendering engine. This is particularly useful for creating PDF documents from web pages, HTML content, and complex web-based forms. The WKHtmltoPdf engine supports the latest HTML5, CSS3, and JavaScript standards, ensuring compatibility with modern web technologies.

Key Features

  • Convert HTML, ASPX, and web pages to PDF seamlessly.
  • Full support for HTML5, CSS3, and JavaScript.
  • Ability to include external assets like CSS, JavaScript, images, and fonts in the PDF.
  • High-quality rendering of complex layouts and styles.
  • Customizable headers, footers, and page numbering.
  • Support for various output file formats including PDF/A, PDF/X.

Benefits

  • Easy integration into existing .NET applications.
  • No need for external dependencies or servers.
  • High-quality and high-performance PDF generation.
  • Comprehensive documentation and support from Iron Software.

For more information, visit IronPDF.


JavaScript Execution

One of the standout features of IronPDF using WKHtmltoPdf is the ability to execute JavaScript within the HTML before conversion, making it possible to create dynamic content and interactive elements in the resulting PDFs. This ensures that even the most interactive and modern web applications can be accurately converted to PDF format.

Execution Options

  • Delayed rendering to account for asynchronous JavaScript operations.
  • Configuration options to control JavaScript execution wait time.
  • Ability to inject custom JavaScript before the PDF conversion.

By leveraging these capabilities, developers can make sure that their PDFs are not only visually compelling but also functionally robust, matching the original web content closely.


For a detailed guide on how to use WKHtmltoPdf with IronPDF, consult our documentation.TOPdfライブラリ

wkhtmltopdf は、HTMLプレーンテキストページを高品質なPDFドキュメントにシームレスに変換するオープンソースのコマンドラインツールです。 C++プログラムにおける機能を活用することで、HTML文字列コンテンツを簡単にPDF形式に変換できます。 C++でwkhtmltopdfライブラリを使用してHTMLページをPDFに変換するステップバイステップのプロセスを掘り下げましょう。

前提条件

C++でHTMLからPDFファイルへのコンバーターを作成するには、次の点を確認する必要があります:

  1. システムにインストールされたGCCまたはClangなどのC++コンパイラ。

  2. wkhtmltopdfライブラリがインストールされました。 公式サイトから最新バージョンをダウンロードできます wkhtmltopdfウェブサイト お使いのオペレーティングシステムの指示に従ってインストールしてください。

  3. C++プログラミングの基礎知識。

Code::BlocksでC++ HtmltoPdfプロジェクトを作成する

Code::BlocksでC++ PDF変換プロジェクトを作成するには、次の手順に従ってください:

  1. Code::Blocks IDEを開いてください。

  2. 「ファイル」メニューに移動して「新規」を選び、「プロジェクト」を選択して新しいプロジェクトウィザードを開きます。

  3. 新しいプロジェクトウィザードで「コンソールアプリケーション」を選択します。

  4. C++ 言語を選択します。

  5. プロジェクトのタイトルと保存したい場所を設定します。 「次へ」をクリックして進みます。

  6. 適切なC++コンパイラとビルドターゲット(例えば、DebugまたはRelease)を選択してください。 「完了」をクリックしてプロジェクトを作成します。

検索ディレクトリの設定

Code::Blocks が必要なヘッダファイルを見つけられるようにするためには、検索ディレクトリを設定する必要があります。

  1. メニューバーの「プロジェクト」メニューをクリックして、「ビルドオプション」を選択します。 「デバッグ」を選択してください。

  2. 「ビルドオプション」ダイアログボックスで、「検索ディレクトリ」タブを選択します。

  3. 「コンパイラ」タブの下にある「追加」ボタンをクリックしてください。

  4. wkhtmltox ヘッダーファイルが配置されているディレクトリに移動します。 (例えば、 C:\Program Files\wkhtmltopdf\include)、そしてそれを選択します。

  5. 最後に、「OK」をクリックしてダイアログボックスを閉じます。

    C++ で HTML を PDF に変換する方法: 図 1 - 検索ディレクトリ

ライブラリのリンク付け

wkhtmltoxライブラリにリンクするには、以下の手順に従ってください:

  1. 再び「プロジェクト」メニューをメニューバーでクリックし、「ビルドオプション」を選択します。 「デバッグ」を選択してください。

  2. 「ビルドオプション」ダイアログボックスで、「リンカー設定」タブを選択します。

  3. 「リンクライブラリ」タブの下にある「追加」ボタンをクリックします。

  4. wkhtmltox ライブラリファイルがあるディレクトリに移動します。 (例えば、C:\Program Files\wkhtmltopdf\lib)適切なライブラリファイルを選択してください。

  5. 「開く」をクリックして、ライブラリをプロジェクトに追加します。

  6. 最後に、「OK」をクリックしてダイアログボックスを閉じます。

    HTMLをPDFに変換する方法(C++): 図2 - ライブラリのリンク

C++でHTMLをPDFに簡単に変換する手順

ステップ 1: HTMLファイルを変換するためのライブラリを含める

始めるには、C++プログラムでwkhtmltopdfライブラリの機能を利用するために必要なヘッダーファイルをインクルードしてください。 次のヘッダーファイルを main.cpp ソースコードファイルの冒頭に含めて、以下の例のようにしてください:


    #include <iostream>
    #include <fstream>
    #include <string>
    #include <wkhtmltox/pdf.h>

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <wkhtmltox/pdf.h>
#include <iostream>
	#include <fstream>
	#include <string>
	#include <wkhtmltox/pdf.h>
VB   C#

ステップ2 コンバーターの初期化

HTML を PDF に変換するには、wkhtmltopdf コンバーターを初期化する必要があります。 コードは次のようになります:


    wkhtmltopdf_init(false);

    wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
    wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
    wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);

    wkhtmltopdf_init(false);

    wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
    wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
    wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);
wkhtmltopdf_init(False)

	wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings()
	wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings()
	wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs)
VB   C#

ステップ3 HTMLコンテンツの設定

さて、PDFに変換する必要があるHTMLコンテンツを提供しましょう。 HTMLファイルを読み込むか、文字列を直接提供することができます。


    string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";   wkhtmltopdf_add_object(converter, os, htmlString.c_str());

    string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";   wkhtmltopdf_add_object(converter, os, htmlString.c_str());
Dim htmlString As String = "<html><body><h1>Hello, World!</h1></body></html>"
wkhtmltopdf_add_object(converter, os, htmlString.c_str())
VB   C#

ステップ 4 HTML を PDF に変換

コンバーターとHTMLコンテンツの準備が整ったら、HTMLをPDFファイルに変換することができます。次のコードスニペットを使用してください:


    wkhtmltopdf_convert(converter);

    wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter)
VB   C#

ステップ5 メモリバッファとして出力を取得

wkhtmltopdf_get_output 関数を使用すると、既存のPDFデータをメモリバッファストリームとして取得することができます。 それはまたPDFの長さも返します。 次の例は、このタスクを実行します:


    const unsigned char* pdfData;
    const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);

    const unsigned char* pdfData;
    const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);
const unsigned Char* pdfData
	Const pdfLength As Integer = wkhtmltopdf_get_output(converter, &pdfData)
VB   C#

ステップ6 PDFファイルを保存する

変換が完了したら、生成されたPDFファイルをディスクに保存する必要があります。 PDFを保存するファイルパスを指定してください。 次に、出力ファイルストリームを使用して、バイナリモードでファイルを開き、pdfDataを書き込みます。 最後に、ファイルを閉じます。以下にコード例を示します:


    const char* outputPath = "file.pdf";
    ofstream outputFile(outputPath, ios::binary);
    outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
    outputFile.close();

    const char* outputPath = "file.pdf";
    ofstream outputFile(outputPath, ios::binary);
    outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
    outputFile.close();
const Char* outputPath = "file.pdf"
	ofstream outputFile(outputPath, ios:=:=binary)
	outputFile.write(reinterpret_cast<const Char*>(pdfData), pdfLength)
	outputFile.close()
VB   C#

ステップ7 クリーンアップ

HTMLをPDFに変換した後、wkhtmltopdfによって割り当てられたリソースをクリーンアップすることが重要です。 以下のコードスニペットを使用してください:


    wkhtmltopdf_destroy_converter(converter);
    wkhtmltopdf_destroy_object_settings(os);
    wkhtmltopdf_destroy_global_settings(gs);
    wkhtmltopdf_deinit();

    cout << "PDF saved successfully." << endl;

    wkhtmltopdf_destroy_converter(converter);
    wkhtmltopdf_destroy_object_settings(os);
    wkhtmltopdf_destroy_global_settings(gs);
    wkhtmltopdf_deinit();

    cout << "PDF saved successfully." << endl;
wkhtmltopdf_destroy_converter(converter)
	wkhtmltopdf_destroy_object_settings(os)
	wkhtmltopdf_destroy_global_settings(gs)
	wkhtmltopdf_deinit()

	cout << "PDF saved successfully." << endl
VB   C#

ステップ8 コードを実行してPDFファイルを生成します

では、プロジェクトをビルドし、F9を使用してコードを実行してください。出力はプロジェクトフォルダーに生成および保存されます。 結果として得られたPDFは以下の通りです:

C++でHTMLをPDFに変換する方法:図3 - PDF出力

C#でHTMLファイルをPDFファイルに変換

IronPDF (アイアンPDF)

IronPDF は、開発者がHTMLコンテンツからPDFドキュメントを簡単に生成できる強力な .NET および .NET Core C# ライブラリです。 HTMLウェブページをPDFに変換するプロセスを簡素化する、分かりやすく直感的なAPIを提供しており、さまざまなアプリケーションやユースケースで人気のある選択肢となっています。

IronPDF の主な利点の一つは、その汎用性です。 それはシンプルなHTMLドキュメントの変換だけでなく、CSSスタイリング、JavaScriptインタラクション、さらには動的コンテンツを含む複雑なウェブページの変換もサポートしています。 さらに、変換メソッドへの迅速なアクセスにより、さまざまなPDFコンバータを開発することができます。

Here is the Japanese translation of the provided text:

以下は、変換するためのコード例です HTML文字列をPDFに変換 C#でIronPDFを使用:


    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create PDF content from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");

    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create PDF content from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");
Imports IronPdf

	' Instantiate Renderer
	Private renderer = New ChromePdfRenderer()

	' Create PDF content from a HTML string using C#
	Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

	' Export to a file or Stream
	pdf.SaveAs("output.pdf")
VB   C#

PDF出力:

C++でHTMLをPDFに変換する方法: 図 4 - IronPDF 出力

HTMLファイル、WebページのURL、および画像をPDFに変換する方法の詳細については、こちらをご覧ください コード例ページ.

IronPDFを使用すると、.NET Framework言語でHTMLコンテンツからPDFファイルを生成することが簡単になります。 直感的なAPIと豊富な機能セットのおかげで、変換を必要とする開発者にとって貴重なツールとなります。 HTMLからPDF C#プロジェクトで。 レポート、請求書、または正確なHTMLからPDFへの変換を必要とする他のドキュメントの生成であれ、IronPDFは信頼性が高く効率的なソリューションです。

IronPDFは開発目的で無料ですが、商用利用にはライセンスが必要です。 また、以下を提供します 無料試用 商業用途で完全な機能をテストするため。 ソフトウェアは次の場所からダウンロードできます これ.

< 以前
C++でPDFファイルを読み取る方法
次へ >
Puppeteerを使用してNode.jsでHTMLをPDFに変換する方法

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 10,659,073 View Licenses >