create PDFExporter exporter implementation.

this works, with included version of HiQPdf. (or i may be using another version..ARGH DLL HELL)
This commit is contained in:
chris.watts90@outlook.com 2018-02-28 19:31:52 +00:00
parent 27a6ae4631
commit d16d01ad3e
6 changed files with 137 additions and 65 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -30,17 +30,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Syncfusion.Compression.Base, Version=14.1451.0.46, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL"> <Reference Include="HiQPdf">
<HintPath>..\packages\Syncfusion.HtmlToPdfConverter451.14.1.0.46\lib\net451\Syncfusion.Compression.Base.dll</HintPath> <HintPath>.\HiQPdf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Syncfusion.HtmlConverter.Base, Version=14.1451.0.46, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.HtmlToPdfConverter451.14.1.0.46\lib\net451\Syncfusion.HtmlConverter.Base.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Syncfusion.Pdf.Base, Version=14.1451.0.46, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
<HintPath>..\packages\Syncfusion.HtmlToPdfConverter451.14.1.0.46\lib\net451\Syncfusion.Pdf.Base.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@ -62,9 +53,6 @@
<Name>Interfaces</Name> <Name>Interfaces</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -1,22 +1,74 @@
using System.IO; using System;
using System.Diagnostics;
using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using HiQPdf;
using Interfaces; using Interfaces;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
namespace PDFExporter namespace PDFExporter
{ {
public class PdfExporter:IFileExporter public class PdfExporter:IFileExporter
{ {
private Stream str = new MemoryStream(); //private Stream str = new MemoryStream();
public Stream Export(string fileContent, string baseUrl) public void Export(string fileContent, string baseUrl, out Stream exportStream)
{ {
var t = new Thread(()=>DoExport(fileContent, baseUrl)); //Stream pdfExportStream = new MemoryStream();
t.SetApartmentState(ApartmentState.STA); //var t = new Thread(new ParameterizedThreadStart(DoExport));//()=>DoExport(fileContent, baseUrl));
t.Start(); //var t2 = Task.Factory.StartNew(() => DoExport(fileContent, baseUrl));
t.Join();
return str; //t.SetApartmentState(ApartmentState.STA);
//t.Start();
//t.Join();
//exportStream = t2.Result;
//FileStream fileStream = File.Create("pdfexportertst.pdf", (int)t2.Result.Length);
//// Initialize the bytes array with the stream length and then fill it with data
//byte[] bytesInStream = new byte[t2.Result.Length];
//t2.Result.Read(bytesInStream, 0, bytesInStream.Length);
//// Use write method to write to the file specified above
//fileStream.Write(bytesInStream, 0, bytesInStream.Length);
//fileStream.Flush(true);
//fileStream.Close();
////CopyStream(t2.Result, fs);
//Debug.WriteLine(t2.Result.Length);
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
converter.MediaType = "print";
converter.RunJavaScript = true;
converter.WaitBeforeConvert = 1;
converter.Document.FitPageWidth = true;
converter.Document.ForceFitPageWidth = true;
converter.BrowserZoom = 135;
converter.Document.Properties.CreationDate = DateTime.UtcNow;
// set converter options
converter.Document.PageSize = PdfPageSize.A4;
converter.Document.PageOrientation = PdfPageOrientation.Portrait;
//converter.Options.WebPageWidth = webPageWidth;
//converter.Options.WebPageHeight = webPageHeight;
// create a new pdf document converting an url
exportStream = new MemoryStream();
converter.ConvertHtmlToStream(fileContent, baseUrl, exportStream);
// save pdf document
//doc.Save(Response, false, "Sample.pdf");
// close pdf document
//doc.Close();
}
private void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
} }
public ExportProviderDetails GetDetails() public ExportProviderDetails GetDetails()
@ -27,53 +79,89 @@ namespace PDFExporter
}; };
} }
void DoExport(string fileContent, string baseUrl) //Stream DoExport(string fileContent, string baseUrl)
{ //{
//var pdfExporter = new HtmlToPdfConverter(); // #region MyRegion
//var settings2 = new IEConverterSettings(); // //var pdfExporter = new HtmlToPdfConverter();
//settings2.AdditionalDelay = 2000; // //var settings2 = new IEConverterSettings();
//pdfExporter.ConverterSettings = settings2; // //settings2.AdditionalDelay = 2000;
//PdfDocument doc = pdfExporter.Convert(fileContent, baseUrl); // //pdfExporter.ConverterSettings = settings2;
// //PdfDocument doc = pdfExporter.Convert(fileContent, baseUrl);
var conv = new HtmlToPdfConverter(); // //var conv = new HtmlToPdfConverter();
PdfDocument document = new PdfDocument(); // //PdfDocument document = new PdfDocument();
//Set page margins and dimensions. // ////Set page margins and dimensions.
PdfUnitConvertor convertor = new PdfUnitConvertor(); // //PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(document.PageSettings.Width, PdfGraphicsUnit.Point); // //float width = convertor.ConvertToPixels(document.PageSettings.Width, PdfGraphicsUnit.Point);
float height = convertor.ConvertToPixels(document.PageSettings.Height, PdfGraphicsUnit.Point); // //float height = convertor.ConvertToPixels(document.PageSettings.Height, PdfGraphicsUnit.Point);
var settings = new IEConverterSettings(); // //var settings = new IEConverterSettings();
settings.EnableHyperLink = true; // //settings.EnableHyperLink = true;
settings.EnableJavaScript = true; // //settings.EnableJavaScript = true;
settings.SplitImages = false; // //settings.SplitImages = false;
settings.SplitTextLines = false; // //settings.SplitTextLines = false;
settings.Margin.All = 20.0f; // //settings.Margin.All = 20.0f;
settings.PdfPageSize = PdfPageSize.A4; // //settings.PdfPageSize = PdfPageSize.A4;
conv.ConverterSettings = settings; // //conv.ConverterSettings = settings;
//Convert the URL/HTML string by providing the required width and height. // ////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); // //var doc = conv.Convert(@"http://www.bbc.co.uk", width, height, AspectRatio.KeepWidth);
doc.PageSettings.Size = PdfPageSize.A4; // //doc.PageSettings.Size = PdfPageSize.A4;
doc.PageSettings.SetMargins((float)20.0f); // //doc.PageSettings.SetMargins((float)20.0f);
doc.Save("c:\\zzz\\test3.pdf"); // //doc.Save("c:\\zzz\\test3.pdf");
//doc.Close(true); // ////doc.Close(true);
MemoryStream ms = new MemoryStream();
doc.Save(ms); // #endregion
doc.Close(true); // // instantiate a html to pdf converter object
ms.CopyTo(str); // HtmlToPdf converter = new HtmlToPdf();
ms.Flush();
ms.Close(); // // set converter options
ms.Dispose(); // converter.Options.PdfPageSize = PdfPageSize.A4;
} // converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
// converter.Options.CssMediaType = HtmlToPdfCssMediaType.Print;
// //converter.Options.WebPageWidth = webPageWidth;
// //converter.Options.WebPageHeight = webPageHeight;
// // create a new pdf document converting an url
// PdfDocument doc = converter.ConvertHtmlString(fileContent, baseUrl);
// doc.Margins = new PdfMargins(20.0f, 20.0f,20.0f,20.0f);
// // save pdf document
// //doc.Save(Response, false, "Sample.pdf");
// // close pdf document
// //doc.Close();
// File.Delete("plugintest.pdf");
// //doc.Save("plugintest.pdf");
// MemoryStream ms = new MemoryStream();
// doc.Save(ms);
// //doc.Save(exportStream);
// //FileStream fs = new FileStream("pdfexporterfs2.pdf", FileMode.Create);
// //CopyStream(ms, fs);
// //fs.Flush(true);
// //fs.Close();
// //fs.Dispose();
// //doc.Save(fs);
// //ms.WriteTo(fs);
// //fs.Flush(true);
// //fs.Close();
// //fs.Dispose();
// doc.Close();
// return ms;
// //ms.CopyTo(str);
// //ms.Flush();
// //ms.Close();
// //ms.Dispose();
//}
} }
} }

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Syncfusion.HtmlToPdfConverter451" version="14.1.0.46" targetFramework="net452" />
</packages>