Friday, December 19, 2014

Convert To PDF Using Pechkin in asp .net.

  1. Download and compile this branch of the Pechkin project: https://github.com/tuespetre/Pechkin (This will solve the DLL hanging problem :)). Or, if you’re lazy, you can download here the needed DLLs. Buid the updated pechkin library
  2. Inside your web application solution add a reference to: Common.Logging.dll and Pechkin.dll.
    Add references
  3. Add to the root of your solution the following DLLs and set from the properties window: Copy to output directory » Copy always.Copy always
  4. Now you can start generating PDF files from HTML strings. Below is a very simple example that does this: 
    protected void btnExportToPDF_Click(object sender, EventArgs e)
    {
        var pechkin = Factory.Create(new GlobalConfig());
        var pdf = pechkin.Convert(new ObjectConfig()
                                .SetLoadImages(true).SetZoomFactor(1.5)
                                .SetPrintBackground(true)
                                .SetScreenMediaType(true)
                                .SetCreateExternalLinks(true), ltrReport.Text);

        //Return the PDF file
        Response.Clear();

        Response.ClearContent();
        Response.ClearHeaders();

        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=test.pdf; size={0}", pdf.Length));
        Response.BinaryWrite(pdf);

        Response.Flush();
        Response.End();
    }

5. After publishing it to IIS, select the application pool for your web site, then right-click and select Advanced Settings... Here make sure to enable 32-bit applications otherwise you will get this error:
Could not load file or assembly ‘Pechkin’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
  1. Application pool settings