Reputation: 508
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:
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
Upvotes: 3
Views: 5952
Reputation: 13
here I give you one code which provide html to pdf file in .net. Install one nuget package which is the Select.HtmlToPdf.NetCore.
private static void Main(string[] args)
{
// instantiate the html to pdf converter
HtmlToPdf converter = new HtmlToPdf();
//get html file path
var html = File.ReadAllText("C:\\Users\\..\\index.html");
// convert the url to pdf
PdfDocument doc = converter.ConvertHtmlString(html);
//save pdf file path
var file = "C:\\Users\\..\\index.pdf";
// save pdf document
doc.Save(file);
// close pdf document
doc.Close();
}
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 749
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");
}
Upvotes: 3
Reputation: 23016
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# ;-)
Upvotes: 1
Reputation: 41422
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.
Upvotes: 1