public static string Run(string pdfPath, string htmlFullPath, int pdfFontSize) { var htmlTemp = Path.Combine(new DirectoryInfo(htmlFullPath).Parent.FullName, Guid.NewGuid().ToString() + ".html"); File.Copy(htmlFullPath, htmlTemp); var pdfUrl = pdfPath; try { #region USING WkHtmlToXSharp.dll // IHtmlToPdfConverter converter = new WkHtmlToPdfConverter(); IHtmlToPdfConverter converter = new MultiplexingConverter(); converter.GlobalSettings.Margin.Top = "0cm"; converter.GlobalSettings.Margin.Bottom = "0cm"; converter.GlobalSettings.Margin.Left = "0cm"; converter.GlobalSettings.Margin.Right = "0cm"; converter.ObjectSettings.Page = htmlTemp; converter.ObjectSettings.Web.EnablePlugins = true; converter.ObjectSettings.Web.EnableJavascript = true; converter.ObjectSettings.Web.Background = true; converter.ObjectSettings.Web.LoadImages = true; converter.ObjectSettings.Load.LoadErrorHandling = LoadErrorHandlingType.abort; converter.ObjectSettings.Web.MinimumFontSize = pdfFontSize; Byte[] bufferPDF = converter.Convert(); System.IO.File.WriteAllBytes(pdfUrl, bufferPDF); converter.Dispose(); #endregion } catch (Exception ex) { File.Delete(htmlTemp); logger.Error(ex); return("error." + ex.Message); } File.Delete(htmlTemp); return("ok"); }
public void PrintTables(List <DataTable> aTables, List <string> aDetails, List <string> aFooter, string aLogoPath, string aFilePath, bool aIsThermal = false, bool print = false, bool savepdf = false, string pdfdir = "", bool saveexcel2003 = false, string excel2003dir = "", bool colored = false, bool ForceRTL = false) { try { DateTime StartTime = DateTime.Now; ExportToHTML(aTables, aDetails, aFooter, aFilePath, aIsThermal, (aIsThermal || ForceRTL) ? Reports.ReportsHelper.ReceiptRTL : Reports.ReportsHelper.RTL, colored); //Process p = new Process(); if (print || savepdf) { using (var sr = new StreamReader(aFilePath)) { var str = sr.ReadToEnd(); sr.Close(); using (var wk = new MultiplexingConverter()) { var tmp = wk.Convert(str); if (print) { string PDFFilePath = aFilePath.Replace(".html", ".pdf"); File.WriteAllBytes(PDFFilePath, tmp); PrintPDFs(PDFFilePath); } else if (savepdf) { pdfdir = pdfdir.Replace(".html", ""); File.WriteAllBytes(pdfdir, tmp); } wk.Dispose(); } } //p.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\wkhtmltopdf.exe"; //p.StartInfo.CreateNoWindow = true; //p.StartInfo.UseShellExecute = false; //if (print) //{ // p.StartInfo.Arguments = "-q \"" + aFilePath + "\" \"" + aFilePath + ".pdf\""; // p.Start(); // p.WaitForExit(1000); // PrintPDFs(aFilePath + ".pdf"); //} //else if (savepdf) //{ // p.StartInfo.Arguments = "\"" + aFilePath + "\" \"" + pdfdir + "\""; // p.Start(); // p.WaitForExit(); //} } else if (saveexcel2003) { System.IO.File.Copy(aFilePath, excel2003dir, true); } //TimeSpan ts = DateTime.Now.Subtract(StartTime); //string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", // ts.Hours, ts.Minutes, ts.Seconds, // ts.Milliseconds / 10); //MessageBox.Show("IncreaseBtn_Click=" + elapsedTime); File.Delete(aFilePath); } catch (Exception ex) { MessageBox.Show("Printing Manager ERROR" + ex.Message + ex.ToString()); } }