public async Task ExportHtml_Test()
        {
            var exporter = new HtmlExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportHtml_Test) + ".html");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //此处使用默认模板导出
            var result = await exporter.ExportByTemplate(filePath, GenFu.GenFu.ListOf <ExportTestData>());

            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }
Example #2
0
        /// <summary>
        ///     根据模板导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <TemplateFileInfo> ExportByTemplate <T>(string fileName, T data, string htmlTemplate) where T : class
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("文件名必须填写!", nameof(fileName));
            }

            var exporterAttribute = GetExporterAttribute <T>();
            var exporter          = new HtmlExporter();
            var htmlString        = await exporter.ExportByTemplate(data, htmlTemplate);

            if (exporterAttribute.IsWriteHtml)
            {
                File.WriteAllText(fileName + ".html", htmlString);
            }

            var doc = GetHtmlToPdfDocumentByExporterAttribute(fileName, exporterAttribute, htmlString);

            PdfConverter.Convert(doc);
            var fileInfo = new TemplateFileInfo(fileName, "application/pdf");

            return(fileInfo);
        }