PDF Generator

With an ever increasing demand for PDF-export from our customers, will have now included the basics of PDF-printing into entity commerce. This means that developers are now able to print to PDF with a few lines of code. Here are some code.

Be sure to include the library:

using SelectPdf;

If you use Visual Studio, the intellisense will of course guide you through the rest, but here is an example:

            //var filePath = Server.MapPath("~/site/" + Util.SiteName + "/cache/PDF-" + Guid.NewGuid() + ".pdf");
            //var filename = Guid.NewGuid() + ".pdf";

            var pageSize = PdfPageSize.A4;
            var pdfOrientation = PdfPageOrientation.Portrait;

             var converter = new HtmlToPdf();

            //Set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.JavaScriptEnabled = true;
            converter.Options.MarginTop = 25;
            converter.Options.MarginBottom = 25;
            converter.Options.DrawBackground = true;
            converter.Options.CssMediaType = HtmlToPdfCssMediaType.Print;
            converter.Options.PageBreaksEnhancedAlgorithm = true;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(url);

            //Save pdf document or send to response
            //doc.Save(filePath);
            doc.Save(Response, false, filename);

            // close pdf document
            doc.Close();