56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using DinkToPdf.Contracts;
|
|
using DinkToPdf;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
|
|
namespace New_College.Common
|
|
{
|
|
|
|
public class HtmlToPdfConverter
|
|
{
|
|
private readonly IConverter _pdfConverter;
|
|
public HtmlToPdfConverter()
|
|
{
|
|
var globalSettings = new GlobalSettings
|
|
{
|
|
ColorMode = ColorMode.Color,
|
|
Orientation = Orientation.Portrait,
|
|
PaperSize = PaperKind.A4,
|
|
};
|
|
|
|
var objectSettings = new ObjectSettings
|
|
{
|
|
PagesCount = true,
|
|
HtmlContent = "<h1>Hello World</h1>",
|
|
WebSettings = { DefaultEncoding = "utf-8" },
|
|
};
|
|
|
|
_pdfConverter = new SynchronizedConverter(new PdfTools());
|
|
}
|
|
|
|
public byte[] ConvertHtmlToPdf(string htmlContent)
|
|
{
|
|
try
|
|
{
|
|
var document = new HtmlToPdfDocument()
|
|
{
|
|
GlobalSettings = { PaperSize = PaperKind.A4, Orientation = Orientation.Portrait, DPI = 300 },
|
|
Objects = { new ObjectSettings { HtmlContent = htmlContent } }
|
|
};
|
|
byte[] pdfBytes = _pdfConverter.Convert(document);
|
|
return pdfBytes;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|