1

I am new to C# HTML and PDF converter. I have app that converts HTML to pdf and I will have to add the code to save pdf. I am unsuccessful.

Here is the part of the code. that converts html to pdf. Can somebody help me to figure out how to save the pdf.

private byte[] ConvertHTMLStringToPDF(string html, bool landscape=false)
        {
            string body = string.Format("<!DOCTYPE html><html><head><title>Report</title><link href=\"{0}Content/print.css\" rel=\"stylesheet\" type=\"text/css\" /><link href=\"{0}Content/css/glyphicons-filetypes.css\" rel=\"stylesheet\" type=\"text/css\" /><link href=\"{0}Content/css/glyphicons.css\" rel=\"stylesheet\" type=\"text/css\" /></head><body>{1}</body></html>", BaseUrl, html);

            // Create the PDF converter. Optionally the HTML viewer width can be specified as parameter
            // The default HTML viewer width is 1024 pixels.
            PdfConverter pdfConverter = new PdfConverter(2000);

            // set the license key
            pdfConverter.LicenseKey = "xxxx";

            EvoPdf.ImgConverter imgConverter = new ImgConverter();
            System.Drawing.Image img = imgConverter.GetImageFromHtmlString(body);
            pdfConverter.HtmlViewerHeight = img.Height + 500;
            img.Dispose();

            // set the converter options
            pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Letter;
            pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
            pdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Landscape;
            // set if header and footer are shown in the PDF - optional - default is false 
            pdfConverter.PdfDocumentOptions.ShowHeader = false;
            pdfConverter.PdfDocumentOptions.ShowFooter = false;
            // set if the HTML content is resized if necessary to fit the PDF page width - default is true
            pdfConverter.PdfDocumentOptions.FitWidth = true;

            // set the embedded fonts option - optional - default is false
            pdfConverter.PdfDocumentOptions.EmbedFonts = true;


            byte[] pdfBytes = null;

            if (BaseUrl.Length > 0)
                pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(body, BaseUrl);
               ///pdfBytes = pdfConverter.SavePdfFromHtmlStringToFile(body,BaseUrl,)

              else
                pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(body);

            return pdfBytes;
Gyum Fox
  • 3,287
  • 2
  • 41
  • 71
TD2013
  • 97
  • 1
  • 3
  • 13
  • 1
    Since `pdfBytes` is just a byte array, [this](http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file-in-c) is probably what you need. – Alexander Obersht Oct 02 '15 at 20:22

0 Answers0