RaceLapTimer/RaceLapTimer/PDFExporter/PdfExporter.cs
chris.watts90@outlook.com cf2f5a72ae fix locator to prevent fileload exception in reflection only context for referenced dlls of plugins.
added probing path to app.config to include dlls in the plugins dir.
updated pdf exporter code to the working code. still doesnt work on stream code though.
2017-07-06 16:19:39 +01:00

80 lines
2.4 KiB
C#

using System.IO;
using System.Threading;
using Interfaces;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
namespace PDFExporter
{
public class PdfExporter:IFileExporter
{
private Stream str = new MemoryStream();
public Stream Export(string fileContent, string baseUrl)
{
var t = new Thread(()=>DoExport(fileContent, baseUrl));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return str;
}
public ExportProviderDetails GetDetails()
{
return new ExportProviderDetails
{
Type = "PDF"
};
}
void DoExport(string fileContent, string baseUrl)
{
//var pdfExporter = new HtmlToPdfConverter();
//var settings2 = new IEConverterSettings();
//settings2.AdditionalDelay = 2000;
//pdfExporter.ConverterSettings = settings2;
//PdfDocument doc = pdfExporter.Convert(fileContent, baseUrl);
var conv = new HtmlToPdfConverter();
PdfDocument document = new PdfDocument();
//Set page margins and dimensions.
PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(document.PageSettings.Width, PdfGraphicsUnit.Point);
float height = convertor.ConvertToPixels(document.PageSettings.Height, PdfGraphicsUnit.Point);
var settings = new IEConverterSettings();
settings.EnableHyperLink = true;
settings.EnableJavaScript = true;
settings.SplitImages = false;
settings.SplitTextLines = false;
settings.Margin.All = 20.0f;
settings.PdfPageSize = PdfPageSize.A4;
conv.ConverterSettings = settings;
//Convert the URL/HTML string by providing the required width and height.
var doc = conv.Convert(@"http://www.bbc.co.uk", width, height, AspectRatio.KeepWidth);
doc.PageSettings.Size = PdfPageSize.A4;
doc.PageSettings.SetMargins((float)20.0f);
doc.Save("c:\\zzz\\test3.pdf");
//doc.Close(true);
MemoryStream ms = new MemoryStream();
doc.Save(ms);
doc.Close(true);
ms.CopyTo(str);
ms.Flush();
ms.Close();
ms.Dispose();
}
}
}