0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#VB.NET HTMLをPDFに変換する方法

0
Posted at

HTMLファイルをPDFに変換すると、オフラインで読み取ることができるほか、そのコンテンツを保存して忠実にフォーマットできるなど、多くの利点があります。Spire.PDFは、HTMLをPDFに変換する2つの方法を提供します。1つはQT Webプラグインを使用する方法で、もう1つはプラグインを使用しない方法です。QTプラグインを使用して変換を行うことをお勧めします。

次のセクションでは、QTプラグインの有無にかかわらずSpire.PDF for .NETを使用してHTMLWebページ(URL)またはHTML文字列をPDFドキュメントにレンダリングする方法を示します。

QTプラグインを使用してURLをPDFに変換する
QTプラグインを使用してHTML文字列をPDFに変換する
プラグインなしでURLをPDFに変換する
プラグインなしでHTML文字列をPDFに変換する

Spire.PDF for.NETをインストールします

まず、Spire.PDF for.NETパッケージに含まれているDLLファイルを.NETプロジェクトの参照として追加する必要があります。DLLファイルは、このリンクからダウンロードするか、NuGetを介してインストールできます。

PM> Install-Package Spire.PDF

プラグインをダウンロードする

プラグイン方式を選択した場合は、以下のリンクからご使用のオペレーティングシステムに適合するプラグインをダウンロードしてください。

Windows x86
Windows x64
Linux x64
Mac x64
パッケージをディスクに解凍して、「プラグイン」フォルダを取得します。この例では、プラグインをパス「F:\ Libraries \ Plugin \ plugins-windows-x64\plugins」の下に保存しました。

また、プロジェクトの「プラットフォームターゲット」をそれに応じてx64またはx86に設定することをお勧めします。

QTプラグインを使用してURLをPDFに変換する

以下は、QTプラグインでSpire.PDFを使用してURLをPDFに変換する手順です。

変換するURLパスを指定します。
生成されたPDFファイルのパスを指定します。
プラグインパスを指定し、HtmlConverter.PluginPathプロパティの値として割り当てます。
HtmlConverter.Convert(string url、string fileName、bool enableJavaScript、int timeout、SizeF pageSize、PdfMargins margins) メソッドを呼び出して、URLをPDFドキュメントに変換します。

C#

using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter.Qt;
using System.Drawing;

namespace ConvertUrlToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //URLパスを指定する
            string url = "https://www.wikipedia.org/";

            //出力ファイルのパスを指定する
            string fileName = "UrlToPdf.pdf";

            //プラグインパスを指定する
            string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

            //プラグインパスを設定する
            HtmlConverter.PluginPath = pluginPath;

            //URLをPDFに変換する
            HtmlConverter.Convert(url, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0));
        }
    }
}

VB.NET

Imports Spire.Pdf.Graphics
Imports Spire.Pdf.HtmlConverter.Qt
Imports System.Drawing
 
Namespace ConvertUrlToPdf
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'URLパスを指定する
            Dim url As String =  "https://www.wikipedia.org/" 
 
            '出力ファイルのパスを指定する
            Dim fileName As String =  "UrlToPdf.pdf" 
 
            'プラグインパスを指定する
            Dim pluginPath As String =  "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins" 
 
            'プラグインパスを設定する
            HtmlConverter.PluginPath = pluginPath
 
            'URLをPDFに変換する
            HtmlConverter.Convert(url, fileName, True, 100000, New Size(1080, 1000), New PdfMargins(0))
        End Sub
    End Class
End Namespace

QTプラグインを使用してHTML文字列をPDFに変換する

以下は、QTプラグインでSpire.PDFを使用してHTML文字列をPDFに変換する手順です。

.htmlファイルからHTML文字列を取得します。
生成されたPDFファイルのパスを指定します。
プラグインパスを指定し、HtmlConverter.PluginPathプロパティの値として割り当てます。
HtmlConverter.Convert(string htmlString、string fileName、bool enableJavaScript、int timeout、SizeF pageSize、PdfMargins margins、Spire.Pdf.HtmlConverter.LoadHtmlType htmlType) メソッドを呼び出して、HTML文字列をPDFドキュメントに変換します。

注:PDFで正しくレンダリングできるのは、インラインCSSスタイルと内部CSSスタイルのみです。外部CSSスタイルシートをお持ちの場合は、インラインまたは内部CSSスタイルに変換してください。

C#

using System.IO;
using Spire.Pdf.HtmlConverter.Qt;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace ConvertHtmlStringToPdfWithPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //.htmlファイルからHTML文字列を取得する
            string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

            //出力ファイルのパスを指定する
            string fileName = "HtmlStringToPdf.pdf";

            //プラグインパスを指定する
            string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

            //プラグインパスを設定する
            HtmlConverter.PluginPath = pluginPath;

            //HTML文字列をPDFに変換する
            HtmlConverter.Convert(htmlString, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode);
        }
    }
}

VB.NET

Imports System.IO
Imports Spire.Pdf.HtmlConverter.Qt
Imports System.Drawing
Imports Spire.Pdf.Graphics
 
Namespace ConvertHtmlStringToPdfWithPlugin
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '.htmlファイルからHTML文字列を取得する
            Dim htmlString As String =  File.ReadAllText("C:\Users\Administrator\Desktop\Document\Html\Sample.html") 
 
            '出力ファイルのパスを指定する
            Dim fileName As String =  "HtmlStringToPdf.pdf" 
 
            'プラグインパスを指定する
            Dim pluginPath As String =  "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins" 
 
            'プラグインパスを設定する
            HtmlConverter.PluginPath = pluginPath
 
            'HTML文字列をPDFに変換する
            HtmlConverter.Convert(htmlString, fileName, True, 100000, New Size(1080, 1000), New PdfMargins(0), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode)
        End Sub
    End Class
End Namespace

プラグインなしでURLをPDFに変換する

以下は、プラグインなしでSpire.PDFを使用してURLをPDFに変換する手順です。

PdfDocumentオブジェクトを作成します。
PdfPageSettingsオブジェクトを作成し、それを介してページサイズとマージンを設定します。
PdfHtmlLayoutFormatオブジェクトを作成し、そのIsWaitingプロパティをtrueに設定します。
変換するURLパスを指定します。
PdfDocument.LoadFromHTML() メソッドを使用してURLパスからHTMLをロードします。
PdfDocument.SaveToFile() メソッドを使用してドキュメントをPDFファイルに保存します。

C#

using System;
using Spire.Pdf;
using System.Threading;
using Spire.Pdf.HtmlConverter;
using System.Drawing;

namespace ConverUrlToPdfWithoutPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //PdfDocumentオブジェクトを作成する
            PdfDocument doc = new PdfDocument();

            //PdfPageSettingsオブジェクトを作成する
            PdfPageSettings setting = new PdfPageSettings();

            //オブジェクトを介してページサイズと余白を節約する
            setting.Size = new SizeF(1000, 1000);
            setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

            //PdfHtmlLayoutFormatオブジェクトを作成する
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

            //IsWaitingプロパティをtrueに設定する
            htmlLayoutFormat.IsWaiting = true;

            //変換するURLパスを指定する
            String url = "https://www.Wikipedia.com/";

            //LoadFromHTMLメソッドを使用してURLパスからHTMLをロードする
            Thread thread = new Thread(() =>
            { doc.LoadFromHTML(url, true, true, false, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            //ドキュメントをPDFファイルとして保存する
            doc.SaveToFile("UrlToPdf.pdf");
            doc.Close();
        }
    }
}

VB.NET

Imports System
Imports Spire.Pdf
Imports System.Threading
Imports Spire.Pdf.HtmlConverter
Imports System.Drawing
 
Namespace ConverUrlToPdfWithoutPlugin
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'PdfDocumentオブジェクトを作成する
            Dim doc As PdfDocument =  New PdfDocument() 
 
            'PdfPageSettingsオブジェクトを作成する
            Dim setting As PdfPageSettings =  New PdfPageSettings() 
 
            'オブジェクトを介してページサイズと余白を節約する
            setting.Size = New SizeF(1000, 1000)
            setting.Margins = New Spire.Pdf.Graphics.PdfMargins(20)
 
            'PdfHtmlLayoutFormatオブジェクトを作成する
            Dim htmlLayoutFormat As PdfHtmlLayoutFormat =  New PdfHtmlLayoutFormat() 
 
            'IsWaitingプロパティをtrueに設定する
            htmlLayoutFormat.IsWaiting = True
 
            '変換するURLパスを指定する
            Dim url As String =  "https://www.Wikipedia.com/" 
 
            'LoadFromHTMLメソッドを使用してURLパスからHTMLをロードする
            Thread thread = New Thread(() =>
            {
            	 doc.LoadFromHTML(url, True, True, False, setting, htmlLayoutFormat) 
            }
)
            thread.SetApartmentState(ApartmentState.STA)
            thread.Start()
            thread.Join()
 
            'ドキュメントをPDFファイルとして保存する
            doc.SaveToFile("UrlToPdf.pdf")
            doc.Close()
        End Sub
    End Class
End Namespace

プラグインなしでHTML文字列をPDFに変換する

以下は、プラグインなしでSpire.PDFを使用してHTML文字列をPDFに変換する手順です。

PdfDocumentオブジェクトを作成します。
PdfPageSettingsオブジェクトを作成し、それを介してページサイズとマージンを設定します。
PdfHtmlLayoutFormatオブジェクトを作成し、そのIsWaitingプロパティをtrueに設定します。
.htmlファイルからHTML文字列を読み取ります。
PdfDocument.LoadFromHTML() メソッドを使用してHTML文字列からHTMLをロードします。
PdfDocument.SaveToFile() メソッドを使用してドキュメントをPDFファイルに保存します。

C#

using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System.IO;
using System.Threading;
using System.Drawing;

namespace ConvertHtmlStringToPdfWithoutPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //PdfDocumentオブジェクトを作成する
            PdfDocument doc = new PdfDocument();

            //PdfPageSettingsオブジェクトを作成する
            PdfPageSettings setting = new PdfPageSettings();

            //オブジェクトを介してページサイズと余白を節約する
            setting.Size = new SizeF(1000, 1000);
            setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

            //PdfHtmlLayoutFormatオブジェクトを作成する
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

            //IsWaitingプロパティをtrueに設定する
            htmlLayoutFormat.IsWaiting = true;

            //.htmlファイルからhtml文字列を読み取る
            string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

            //LoadFromHTMLメソッドを使用してhtml文字列からHTMLをロードする
            Thread thread = new Thread(() =>
            { doc.LoadFromHTML(htmlString, true, true, false, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            //PDFファイルとして保存する
            doc.SaveToFile("HtmlStringToPdf.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.HtmlConverter
Imports System.IO
Imports System.Threading
Imports System.Drawing
 
Namespace ConvertHtmlStringToPdfWithoutPlugin
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'PdfDocumentオブジェクトを作成する
            Dim doc As PdfDocument =  New PdfDocument() 
 
            'PdfPageSettingsオブジェクトを作成する
            Dim setting As PdfPageSettings =  New PdfPageSettings() 
 
            'オブジェクトを介してページサイズと余白を節約する
            setting.Size = New SizeF(1000, 1000)
            setting.Margins = New Spire.Pdf.Graphics.PdfMargins(20)
 
            'PdfHtmlLayoutFormatオブジェクトを作成する
            Dim htmlLayoutFormat As PdfHtmlLayoutFormat =  New PdfHtmlLayoutFormat() 
 
            'IsWaitingプロパティをtrueに設定する
            htmlLayoutFormat.IsWaiting = True
 
            '.htmlファイルからhtml文字列を読み取る
            Dim htmlString As String =  File.ReadAllText("C:\Users\Administrator\Desktop\Document\Html\Sample.html") 
 
            'LoadFromHTMLメソッドを使用してhtml文字列からHTMLをロードする
            Thread thread = New Thread(() =>
            {
            	 doc.LoadFromHTML(htmlString, True, True, False, setting, htmlLayoutFormat) 
            }
)
            thread.SetApartmentState(ApartmentState.STA)
            thread.Start()
            thread.Join()
 
            'PDFファイルとして保存する
            doc.SaveToFile("HtmlStringToPdf.pdf")
        End Sub
    End Class
End Namespace

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除する場合、または機能制限を取り除く場合は、30日間有効な一時ライセンスについて営業担当者にお問い合わせください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?