summary
How to convert HTML file to PDF file?Some recruitment online resume into doc file, can not be used directly, this caused great trouble, then it also has a format, that is HTML format. Export the file to HTML format, and then convert it to PDF file, so that it can be used directly. Usually in the project is also a lot of such requirements, need to convert the content into PDF file.
Now let’s take a look at how to convert HTML to PDF using itextsharp.
code implementation
1. Nuget installs itextsharp.
using iTextSharp.text;
using iTextSharp.text.pdf;
2. Convert HTML document to PDF.
/// <summary>
/// Switch Html Document to pdf
/// </summary>
/// <param name="htmlText"></param>
/// <returns></returns>
public byte[] ConvertHtmlTextToPDF(string htmlText)
{
if (string.IsNullOrEmpty(htmlText))
return null;
//to avoid when the htmlText without any html tag tag plain text, to PDF will hang, so all with <p> tag
htmlText = "<p>" + htmlText + "</p>";
using (var outputStream = new MemoryStream())
{
byte[] data = Encoding.UTF8.GetBytes(htmlText);
var msInput = new MemoryStream(data);
var doc = new Document();//pdf file,default A4 formate
var writer = PdfWriter.GetInstance(doc, outputStream);
doc.Open();
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
var pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
var action = PdfAction.GotoLocalPage(1, pdfDest, writer);
writer.SetOpenAction(action);
doc.Close();
msInput.Close();
outputStream.Close();
return outputStream.ToArray();
}
}
3. Unicode font support.
/// <summary>
/// Unicode font support
/// </summary>
public class UnicodeFontFactory : FontFactoryImp
{
public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
{
//var chineseFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "msyh.ttc,0");
//BaseFont baseFont = BaseFont.CreateFont(@"c:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont baseFont = BaseFont.CreateFont(@"c:\Windows\Fonts\SIMHEI.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//var baseFont = BaseFont.CreateFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
return new Font(baseFont, size, style, color);
}
}
4. Call build.
string content = temp.Content;
foreach (var dict in dicts)
{
content = content.Replace("{{" + dict.Key + "}}", dict.Value);
}
var path = _esignInfo.Value.ContractPath;
//if (entity.ContractType == ContractType.First)
//{
// path += "/" + appId + "/Agreements";
//}
entity.OriginalFileUrl = _pdfHelper.WritePdfFile(content, contractNo, path, "PDF");
bool isSucc = !String.IsNullOrEmpty(entity.OriginalFileUrl);
Read More:
- How to Use Annotations in Flutter & Dart
- How to Solve C++ Error: “no matching function for call to ‘atoi/atof’”
- Grep: How to Find All the Files Containing a String in Linux
- How to Uncompress 7z files on Ubuntu, Debian, Fedora
- How to Download APK Files from Google Play Store
- C#: How to get the value or text value of the select drop-down list
- Docker -v Directory Mount (How to Use)
- How to Use Apt get Command Under Mac OSX
- How to Solve Error: Address already in use
- C# WPF Framework: How to Build Caliburn.Micro Quickly
- [Solved] Linux C++ warning: ISO C++ forbids converting a string constant to ‘char*‘ [-Wwrite-strings]
- C# implementation of TXT document to table example code
- [Solved] Linux — 9 — txt files are copied from windows to Linux and read error
- Using common file upload to upload files in SSH project
- How to Export All data in DataGridView to Local Excel
- [Solved] Some index files failed to download.They have been ignored, or old ones used instead
- Sublime text 3 compiles and executes C/C++ programs directly
- How to Solve Error: “Hdaudio hdaudioc0d2: unable to bind codec”
- WPF: How to Reference Font Resource File
- [Solved] Node uploads files to FTP server error: timed out while making data connection