NEWS

[C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform

[C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
Đăng bởi: Thảo Meo - Lượt xem: 1581 22:07:53, 23/04/2023DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách convert file mã HTML thành File Pdf trên winform.

[C#] How Convert HTML to PDF winform

html_pdf_converter

Đầu tiên, các bạn cài đặt thư viện Pechkin từ Nuget vào project của bạn.

NuGetInstall-Package Pechkin.Synchronized -Version 0.5.8.1

Pechkin là  .NET Wrapper cho DLL  WkHtmlToPdf, thư viện sử dụng công cụ Webkit để chuyển đổi các trang HTML thành PDF.

Cách tạo một file PDF từ mã HTML string.

byte[] pdfContent = new SimplePechkin(new GlobalConfig()).Convert("<html><body><h1>Hello world!</h1></body></html>");

để hàm trên nó convert string HTML thì 1 byte Array, chúng ta sẽ dùng thêm hàm dưới đây để ghi byte[] thành File PDF.

public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
    try
    {
        // Open file for reading
        FileStream _FileStream = new FileStream(_FileName, FileMode.Create, FileAccess.Write);
        // Writes a block of bytes to this stream using data from  a byte array.
        _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

        // Close file stream
        _FileStream.Close();

        return true;
    }
    catch (Exception _Exception)
    {
        Console.WriteLine("Exception caught in process while trying to save : {0}", _Exception.ToString());
    }
    
    return false;
}

Viết cách hoàn tất code như sau:

byte[] pdfBuffer = new SimplePechkin(new GlobalConfig()).Convert("<html><body><h1>Hello world!</h1></body></html>");

// Folder where the file will be created 
string directory = "C:\Users\sdkca\Desktop\";
// Name of the PDF
string filename = "laptrinhvb.pdf";

if (ByteArrayToFile(directory + filename, pdfBuffer))
{
    Console.WriteLine("PDF Succesfully created");
}
else
{
    Console.WriteLine("Cannot create PDF");
}

Ngoài ra, các bạn cũng có thể cấu hình các tham số khi chuyển đổi file HTML sang PDF như setting: Margin, Page Landscape hoặc portail...

// create global configuration object
GlobalConfig gc = new GlobalConfig();

// set it up using fluent notation
// Remember to import the following type:
//     using System.Drawing.Printing;
//
// a new instance of Margins with 1-inch margins.
gc.SetMargins(new Margins(100, 100, 100, 100))
    .SetDocumentTitle("Test document")
    .SetPaperSize(PaperKind.Letter);

// Create converter
IPechkin pechkin = new SynchronizedPechkin(gc);

// Create document configuration object
ObjectConfig configuration = new ObjectConfig();


string HTML_FILEPATH = "C:/Users/sdkca/Desktop/example.html";

// and set it up using fluent notation too
configuration
.SetAllowLocalContent(true)
.SetPageUri(@"file:///" + HTML_FILEPATH);


byte[] pdfContent = pechkin.Convert(configuration);

 

Thanks for waching!

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
Đăng bởi: Thảo Meo - Lượt xem: 1581 22:07:53, 23/04/2023DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.