Beispiel #1
0
        /// <summary>
        ///     获取文档转换配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private HtmlToPdfDocument GetHtmlToPdfDocumentByExporterAttribute(string fileName,
                                                                          PdfExporterAttribute pdfExporterAttribute,
                                                                          string htmlString)
        {
            var htmlToPdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ColorMode     = ColorMode.Color,
                    Orientation   = pdfExporterAttribute?.Orientation,
                    PaperSize     = pdfExporterAttribute?.PaperKind,
                    Out           = fileName,
                    DocumentTitle = pdfExporterAttribute?.Name
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PagesCount     = pdfExporterAttribute.IsEnablePagesCount,
                        HtmlContent    = htmlString,
                        WebSettings    = { DefaultEncoding = pdfExporterAttribute?.Encoding.BodyName },
                        Encoding       = pdfExporterAttribute?.Encoding,
                        HeaderSettings = pdfExporterAttribute?.HeaderSettings,
                        FooterSettings = pdfExporterAttribute?.FooterSettings
                    }
                }
            };

            return(htmlToPdfDocument);
        }
        /// <summary>
        ///     导出Pdf
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="template"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task <byte[]> ExportBytesByTemplate <T>(T data, PdfExporterAttribute pdfExporterAttribute, string template) where T : class
        {
            var exporter   = new HtmlExporter();
            var htmlString = await exporter.ExportByTemplate(data, template);

            return(await ExportPdf(pdfExporterAttribute, htmlString));
        }
        /// <summary>
        /// 导出到bytes
        /// </summary>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private async Task <byte[]> ExportPdf(
            PdfExporterAttribute pdfExporterAttribute,
            string htmlString)
        {
            var objSettings = new ObjectSettings
            {
#if !NET461
                HtmlContent = htmlString,
                Encoding    = Encoding.UTF8,
                PagesCount  = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#else
                HtmlText   = htmlString,
                CountPages = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#endif
                WebSettings = { DefaultEncoding = Encoding.UTF8.BodyName },
            };

            if (pdfExporterAttribute?.HeaderSettings != null)
            {
                objSettings.HeaderSettings = pdfExporterAttribute?.HeaderSettings;
            }

            if (pdfExporterAttribute?.FooterSettings != null)
            {
                objSettings.FooterSettings = pdfExporterAttribute?.FooterSettings;
            }

            var htmlToPdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    PaperSize      = pdfExporterAttribute?.PaperKind == PaperKind.Custom
                    ? pdfExporterAttribute.PaperSize : pdfExporterAttribute?.PaperKind,
                    Orientation    = pdfExporterAttribute?.Orientation,
#if !NET461
                    //Out = fileName,
                    ColorMode      = ColorMode.Color,
#else
                    ProduceOutline = true,
#endif
                    DocumentTitle  = pdfExporterAttribute?.Name
                },
                Objects =
                {
                    objSettings
                }
            };

            if (pdfExporterAttribute?.MarginSettings != null)
            {
                htmlToPdfDocument.GlobalSettings.Margins = pdfExporterAttribute?.MarginSettings;
            }


            var result = PdfConverter.Convert(htmlToPdfDocument);

            return(await Task.FromResult(result));
        }
Beispiel #4
0
        /// <summary>
        ///     获取文档转换配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private async Task <ExportFileInfo> ExportPdf(string fileName,
                                                      PdfExporterAttribute pdfExporterAttribute,
                                                      string htmlString)
        {
            var objSettings = new ObjectSettings
            {
#if !NET461
                HtmlContent = htmlString,
                Encoding    = Encoding.UTF8,
                PagesCount  = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#else
                HtmlText   = htmlString,
                CountPages = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#endif
                WebSettings = { DefaultEncoding = Encoding.UTF8.BodyName },
            };

            if (pdfExporterAttribute?.HeaderSettings != null)
            {
                objSettings.HeaderSettings = pdfExporterAttribute?.HeaderSettings;
            }

            if (pdfExporterAttribute?.FooterSettings != null)
            {
                objSettings.FooterSettings = pdfExporterAttribute?.FooterSettings;
            }

            var htmlToPdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    PaperSize   = pdfExporterAttribute?.PaperKind,
                    Orientation = pdfExporterAttribute?.Orientation,

#if !NET461
                    //Out = fileName,
                    ColorMode      = ColorMode.Color,
#else
                    ProduceOutline = true,
#endif
                    DocumentTitle  = pdfExporterAttribute?.Name
                },
                Objects =
                {
                    objSettings
                }
            };
            var result = PdfConverter.Convert(htmlToPdfDocument);

#if NETSTANDARD2_1
            await File.WriteAllBytesAsync(fileName, result);
#else
            File.WriteAllBytes(fileName, result);
#endif

            var fileInfo = new ExportFileInfo(fileName, "application/pdf");
            return(await Task.FromResult(fileInfo));
        }
        /// <summary>
        ///     获取文档转换配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private async Task <ExportFileInfo> ExportPdf(string fileName,
                                                      PdfExporterAttribute pdfExporterAttribute,
                                                      string htmlString)
        {
            var result = await ExportPdf(pdfExporterAttribute, htmlString);

#if NETSTANDARD2_1
            await File.WriteAllBytesAsync(fileName, result);
#else
            File.WriteAllBytes(fileName, result);
#endif

            var fileInfo = new ExportFileInfo(fileName, "application/pdf");
            return(await Task.FromResult(fileInfo));
        }
Beispiel #6
0
        /// <summary>
        /// 导出到bytes
        /// </summary>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private Task <byte[]> ExportPdf(
            PdfExporterAttribute pdfExporterAttribute,
            string htmlString)
        {
            var objSettings = new ObjectSettings
            {
                HtmlContent = htmlString,
                Encoding    = Encoding.UTF8,
                PagesCount  = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
                WebSettings = { DefaultEncoding = Encoding.UTF8.BodyName },
            };

            if (pdfExporterAttribute.HeaderSettings != null)
            {
                objSettings.HeaderSettings = pdfExporterAttribute.HeaderSettings;
            }

            if (pdfExporterAttribute.FooterSettings != null)
            {
                objSettings.FooterSettings = pdfExporterAttribute?.FooterSettings;
            }

            var htmlToPdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    PaperSize     = pdfExporterAttribute.PaperKind == PaperKind.Custom
                    ? pdfExporterAttribute.PaperSize : pdfExporterAttribute.PaperKind,
                    Orientation   = pdfExporterAttribute.Orientation,
                    ColorMode     = ColorMode.Color,
                    DocumentTitle = pdfExporterAttribute.Name
                },
                Objects =
                {
                    objSettings
                }
            };

            if (pdfExporterAttribute.MarginSettings != null)
            {
                htmlToPdfDocument.GlobalSettings.Margins = pdfExporterAttribute.MarginSettings;
            }

            var result = PdfConverter.Convert(htmlToPdfDocument);

            return(Task.FromResult(result));
        }
Beispiel #7
0
        /// <summary>
        /// 导出Pdf
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public async Task <byte[]> ExportListBytesByTemplate <T>(ICollection <T> data, PdfExporterAttribute pdfExporterAttribute, string template) where T : class
        {
            var htmlString = await HtmlExporter.ExportListByTemplate(data, template);

            return(await ExportPdf(pdfExporterAttribute, htmlString));
        }