-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathProgram.cs
More file actions
32 lines (27 loc) · 1.27 KB
/
Program.cs
File metadata and controls
32 lines (27 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// coding: utf - 8
// --------------------------------------------------------------------------
// Copyright(c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------
using System.Diagnostics;
using System.Text;
using iText.Html2pdf;
// function to convert Html string to Pdf document
Action<string, string> ConvertHtmlToPdf = (string htmlString, string outputFilePath) =>
{
using var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString));
using var pdfFileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write);
HtmlConverter.ConvertToPdf(htmlStream, pdfFileStream);
};
var baseDir = Environment.CurrentDirectory;
var htmlContent = File.ReadAllText($"{baseDir}\\HtmlFile\\web-page.html");
var pdfOutputFolder = $"{baseDir}\\Output";
if (!Directory.Exists(pdfOutputFolder))
{
Directory.CreateDirectory(pdfOutputFolder);
}
var pdfOutputPath = $"{pdfOutputFolder}\\converted.pdf";
ConvertHtmlToPdf(htmlContent, pdfOutputPath);
Console.WriteLine($"Pdf convert successfully in {pdfOutputPath}");
Process.Start(new ProcessStartInfo(pdfOutputPath) { UseShellExecute = true });