- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
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
Đầ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!