dotnetcore / dotnetcore/Magicodes.IE
PDF导出扩展
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 495
- PR merge metrics
- No merged PRs in 30d
Description
现在的PdfExporterAttribute很多打印属性无法自定义。比如根据实例属性设置不同的页眉页脚等
ps:下面代码临时凑活的,命名需要规范。
接口或抽象类
``` c#
public abstract class ICustomPdfExporter
{
public virtual void Configure(ref ObjectSettings o)
{
}
}
```
导出pdf时获取下
``` c#
public async Task ExportBytesByTemplateEx(T data, string template) where T : class
{
var html = await _htmlExporter.ExportByTemplate(data, template);
var pdfAttr = data.GetType().GetCustomAttribute() ?? new();
var objSettings = new ObjectSettings
{
HtmlContent = html,
Encoding = Encoding.UTF8,
PagesCount = pdfAttr.IsEnablePagesCount ? true : (bool?)null,
WebSettings = { DefaultEncoding = Encoding.UTF8.BodyName },
};
if (pdfAttr.HeaderSettings != null)
objSettings.HeaderSettings = pdfAttr.HeaderSettings;
if (pdfAttr.FooterSettings != null)
objSettings.FooterSettings = pdfAttr?.FooterSettings;
// 增加 复写打印设置
if (data is ICustomPdfExporter data1)
{
data1.Configure(ref objSettings);
}
var htmlToPdfDocument = new HtmlToPdfDocument
{
GlobalSettings =
{
PaperSize = pdfAttr!.PaperKind == PaperKind.Custom
? pdfAttr.PaperSize : pdfAttr.PaperKind,
Orientation = pdfAttr.Orientation,
ColorMode = ColorMode.Color,
DocumentTitle = pdfAttr.Name,
},
Objects =
{
objSettings
}
};
if (pdfAttr.MarginSettings != null)
{
htmlToPdfDocument.GlobalSettings.Margins = pdfAttr.MarginSettings;
}
return PdfConverter.Convert(htmlToPdfDocument);
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.