HTML到PDFConverter并保存pdf

时间:2015-10-02 19:55:13

标签: c# html

我是C#HTML和PDF转换器的新手。 我有将HTML转换为pdf的应用程序,我将不得不添加代码以保存pdf。 我不成功。

以下是代码的一部分。将html转换为pdf。 有人可以帮我弄清楚如何保存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;

0 个答案:

没有答案