HTML to PDF in c#
I'm trying to create an application that converts a file from the HTML format to the PDF format.
The approach I am using is:
- HTML to XHTML
- XHTML to Formatting Object
- For开发者_运维知识库matting Object to PDF
I'm having a bit of trouble with the whole XHTML to FO(or xsl).
Can you please tell me how to transform the XHTML to FO?
Or maybe a different approapch to the whole HTML to PDF?
Thanks, Catalin
I have write easiest way to write html to pdf code using NRerco Pdf library which available free, Install nuget package
PM > Install-Package NReco.PdfGenerator
Create HtmltoPdf()
{
if (System.IO.File.Exists("HTMLFile.html"))
{
System.IO.File.Delete("HTMLFile.html");
}
System.IO.File.WriteAllText("HTMLFile.html", html);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
if (System.IO.File.Exists("export.pdf"))
{
System.IO.File.Delete("export.pdf");
}
htmlToPdf.GeneratePdfFromFile("HTMLFile.html", null, "export.pdf");
}
Well you could use a HTML to PDF converter via shell, I am sorry I can not rememeber the name of the one I have used in the past, if you have a Google around, you should be able to find a good one.
Searched a lot for my personal stack app project SO2PDF and finally settled with wkhtmltopdf which so far is the best free tool to convert HTML to PDF. Yes I used it with c# ;-)
Here is the different approach. We are going to convert HTML/XML to PDF directly with 3d party tool (it has multiple preference and settings of conversion and doesn't require any external libraries).
1) Download free HTML to PDF SDK from her (it is easy PDF SDK)
2) Use the following code or run Action Center to customize the conversion
using BCL.easyPDF.Printer;
namespace TestPrinter
{
class Program
{
static void Main(string[] args)
{
if(args.Length != 2)
return;
string inputFileName = args[0];
string outputFileName = args[1];
Printer printer = new Printer();
try
{
IEPrintJob printjob = printer.IEPrintJob;
printjob.PrintOut(inputFileName, outputFileName);
}
catch(PrinterException ex)
{
System.Console.WriteLine(ex.Message);
}
finally
{
printer.Dispose();
}
}
}
}
Image: HTML to PDF C# API - Action Center
精彩评论