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

2019-05-23 02:42发布

问题:

I'm looking for some C# code to create a .PDF document on a server from an HTML page that is rendered. The structure and content of the HTML that I want to convert to .PDF is pretty simple (DIV, TABLE, IMAGE, etc.) so I need code that will:

  1. Create a document that is savable by the client browsing (after they click Save As .PDF)
  2. Process a stream of HTML and convert it into .PDF
  3. Handle adding images into the document
  4. Specify the resolution (?) of the document - it will be something I would want to be print quality (high DPI and the images I embed would be high resolution)

I have seen many questions on this but after searching for an hour or so I was unable to find a good starting point to build off of. Any pointers would be great... ideally I'm looking for some source code that I can use and not a purchasable component or a web service that does it all.

回答1:

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#



回答2:

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);



回答3:

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.



回答4:

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



回答5:

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.
    }
}