C# HTML to PDF code for use in a service (on a server)

醉酒当歌 提交于 2019-12-06 11:20:00
jle

Have you looked at wkhtmltopdf? It is open source, though LGPL, but does the best conversion that I can find. It is command line, so should run fine on a server. http://code.google.com/p/wkhtmltopdf/

Also see: Calling wkhtmltopdf to generate PDF from HTML for instructions in c#

WkHtmlToPdf satisfies all of your requirements (including custom DPI and embedded images).

To save a time I recommend to use one of the ready-to-use .NET wrappers over WkHtmlToPdf like PDF Generator for .NET (it's free) because

  • you don't need to download and install wkhtmltopdf at all (everything is packed into one DLL)
  • you can convert HTML to PDF with 1 line of code:

    (new NReco.HtmlToPdfConverter()).GeneratePdf(htmlContent);

I work at Expected Behavior, and we've developed an HTML to PDF API called DocRaptor that uses Prince XML as our PDF rendering engine. Since you mention wanting to create high quality documents, I think our service is a good fit.

We've got example code for C#, which you can find here:

DocRaptor C# example

DocRaptor IS a subscription based service, but we also offer a free plan that allows users to create up to 5 documents per month.

You could give Amyuni WebkitPDF a try. It is a free component that converts HTML to PDF or XPS and the package include sample code for C#.
usual disclaimer applies

The Specific parts of this that would be dependent on SDK, like this one You may also check those manuals, the small bits described in the Server-Side Operation parts of the Native SDK Pages. http://www.pdfonline.com/easypdf/sdk/usermanual/source/running_on_server/asp_sample.htm http://www.pdfonline.com/easypdf/sdk/usermanual/source/using_objects/native_net_printer.htm

Everything else depends on your architecture and how you want to get the file to the machine that runs the SDK. The simplest I suppose would be to use the FileUpload element, a Web Forms element you can add to Websites designed by Visual Studio. From that object you can either save the file locally on the Server, or read it into Memory and process the conversion.

Here's a simple one using FileUpload. It's also easy to switch out to whatever method of File Uploading you want, the only important thing is just to get a byte[] array of the File and its File Extension, which you can then send to PrintOut3(). Or you can save the file locally onto the server to use PrintJob or PrintJob2.

//Upload and convert a File when a Button is pushed
protected void Button1_Click(object sender, EventArgs e)
{
    if(FileUpload1.HasFile)
    {
        string iEXT = Path.GetExtension(FileUpload1.FileName);
        byte[] iMEM = FileUpload1.FileBytes;
        byte[] oMEM;

        Printer oPrin = new Printer();
        PrintJob oPJob = oPrin.PrintJob;

        try
        {
            oMEM = oPJob.PrintOut3(iMEM, iEXT);
        }
        catch (PrinterException ex)
        {
            //Perform your desired Error Handling
        }
        finally
        {
            oPrin.Dispose();
        }

        // Save oMEM as Desired, or use it how you see fit.
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!