First version of the report generating engine finished, Both front and back pages are merged into one PDF. Seems the user's screen resolution and/or size affect the size of the PDF document, I feel this is a sort of bug that must be fixed at some point.

This commit is contained in:
2017-05-08 19:51:45 -05:00
parent 15abab5f50
commit 718562222d
10 changed files with 170 additions and 143 deletions
+43 -28
View File
@@ -1,50 +1,65 @@
using System;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web.UI;
using Pechkin;
using Spire.Pdf;
namespace AdvertsingProfitControl
{
internal class ReportGenerator
{
public readonly string Version = "1.0.0.0";
public static readonly string Version = "0.5.0.0";
/// <summary>
/// Generates a full report, and places the file on the user's desktop for later viewing.
/// </summary>
/// <param name="date"></param>
public void GeneratePdfReport(DateTime date)
{
var stringWriter = new StringWriter();
var writer = new HtmlTextWriter(stringWriter);
//Check to see if the report already exists.
var reportFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + date.ToString("yyyy MMMM dd") + " Report.pdf";
if (File.Exists(reportFileName))
{
var argument = "/select, \"" + reportFileName + "\"";
System.Diagnostics.Process.Start("explorer.exe", argument);
return;
}
var front = new FrontPageGenerator();
var back = new BackPageGenerator();
front.BuildReportFrontPage(date, ref writer);
back.BuildReportBackPage(date, ref writer);
using (var streamWriter = new StreamWriter("C:\\Users\\Crypto\\Desktop\\Full.html"))
{
streamWriter.WriteLine(stringWriter.ToString());
}
front.GenerateFrontPage(date);
back.GenerateBackPage(date);
//Merge the PDFs
GenerateFullReport(reportFileName);
}
public void RenderHtmlCodeToImage(string path, string targetPath)
/// <summary>
/// Merges the front and back PDF files into one and places the new file on the user's desktop.
/// Deletes the front and back pages when done.
/// </summary>
/// <param name="targetPath"></param>
private static void GenerateFullReport(string targetPath)
{
var htmlCode = File.ReadAllLines(path);
var code = htmlCode.Aggregate("", (current, line) => current + "\r\n" + line);
try
{
var pechkin = new SimplePechkin(new GlobalConfig().SetMargins(50,0,0,0).SetPaperSize(PaperKind.A4).SetPaperOrientation(true).SetCopyCount(1));
var pdf = pechkin.Convert(new ObjectConfig().SetZoomFactor(1.25), code);
//var config = new GlobalConfig().SetPaperSize(PaperKind.A4).SetPaperOrientation(true);
//var pdfBuf = new SimplePechkin(config).Convert(code);
using (var fs = new FileStream(targetPath, FileMode.Create, FileAccess.Write))
{
fs.Write(pdf, 0, pdf.Length);
}
var pdfdocs = new PdfDocument[2];
pdfdocs[0] = new PdfDocument();
pdfdocs[0].LoadFromFile(GlobalClasses.LogFilePath + "Front.pdf", FileFormat.PDF);
pdfdocs[1] = new PdfDocument();
pdfdocs[1].LoadFromFile(GlobalClasses.LogFilePath + "Back.pdf", FileFormat.PDF);
pdfdocs[0].AppendPage(pdfdocs[1]);
pdfdocs[0].SaveToFile(targetPath);
pdfdocs[0].Close();
pdfdocs[1].Close();
File.Delete(GlobalClasses.LogFilePath + "Front.pdf");
File.Delete(GlobalClasses.LogFilePath + "Back.pdf");
var argument = "/select, \"" + targetPath + "\"";
System.Diagnostics.Process.Start("explorer.exe", argument);
}
finally
catch (IOException e)
{
}
}
finally
{
}
}
}
}