/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { D3jsLib.PdfStyle style = null; string filePath = null; D3jsLib.Report report = null; bool print = false; if (!DA.GetData <string>(0, ref filePath)) { return; } if (!DA.GetData <D3jsLib.Report>(1, ref report)) { return; } if (!DA.GetData <D3jsLib.PdfStyle>(2, ref style)) { return; } if (!DA.GetData <bool>(3, ref print)) { return; } if (print) { PrintPDF(report, style, filePath); } }
private void PrintPDF(D3jsLib.Report report, D3jsLib.PdfStyle style, string filePath) { HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(report.HtmlString); HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='gridster-box']"); foreach (HtmlNode n in nodes) { n.InnerHtml = ""; } // attempt to move *dep file D3jsLib.Utilities.ChartsUtilities.MoveDepFile(); // create converter SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); // set converter options SelectPdf.HtmlToPdfOptions options = converter.Options; options.PdfPageOrientation = style.Orientation; options.PdfPageSize = style.Size; options.JpegCompressionLevel = style.Compression; options.JavaScriptEnabled = true; options.EmbedFonts = true; options.KeepImagesTogether = true; options.KeepTextsTogether = true; options.AutoFitHeight = style.VerticalFit; options.AutoFitWidth = style.HorizontalFit; options.MarginTop = style.MarginTop; options.MarginRight = style.MarginRight; options.MarginBottom = style.MarginBottom; options.MarginLeft = style.MarginLeft; // created unescaped file path removes %20 from path etc. string finalFilePath = filePath; Uri uri = new Uri(filePath); string absoluteFilePath = Uri.UnescapeDataString(uri.AbsoluteUri); if (Uri.IsWellFormedUriString(absoluteFilePath, UriKind.RelativeOrAbsolute)) { Uri newUri = new Uri(absoluteFilePath); finalFilePath = newUri.LocalPath; } try { // convert html to document object and save SelectPdf.PdfDocument pdfDoc = converter.ConvertHtmlString(htmlDoc.DocumentNode.InnerHtml); pdfDoc.Save(finalFilePath); pdfDoc.Close(); } catch { MessageBox.Show("Printing failed. Is file open in another application?"); } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { D3jsLib.PdfStyle style = null; string filePath = null; bool print = false; if (!DA.GetData <string>(0, ref filePath)) { return; } if (!DA.GetData <D3jsLib.PdfStyle>(1, ref style)) { return; } if (!DA.GetData <bool>(2, ref print)) { return; } if (print) { bool printed = false; Report.MandrillWindow win = null; Mandrill_LaunchWindow winComponent = null; foreach (IGH_DocumentObject obj in Grasshopper.Instances.ActiveCanvas.Document.Objects) { if (obj.GetType() == typeof(Mandrill_LaunchWindow)) { winComponent = (Mandrill_LaunchWindow)obj; if (winComponent != null) { win = winComponent.GetWindow(); if (win != null) { win.Print(filePath, style); printed = true; break; } } } } if (!printed) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "There is either no Mandrill Window Launch component on canvas or one doesn't have an open Window."); } } }
private void PrintPDF(D3jsLib.Report report, D3jsLib.PdfStyle style, string filePath) { HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(report.HtmlString); HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='gridster-box']"); foreach (HtmlNode n in nodes) { n.InnerHtml = ""; } // create converter SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); // set converter options SelectPdf.HtmlToPdfOptions options = converter.Options; options.PdfPageOrientation = style.Orientation; options.PdfPageSize = style.Size; options.JpegCompressionLevel = style.Compression; options.JavaScriptEnabled = true; options.EmbedFonts = true; options.KeepImagesTogether = true; options.KeepTextsTogether = true; options.AutoFitHeight = style.VerticalFit; options.AutoFitWidth = style.HorizontalFit; options.MarginTop = style.MarginTop; options.MarginRight = style.MarginRight; options.MarginBottom = style.MarginBottom; options.MarginLeft = style.MarginLeft; try { // convert html to document object and save SelectPdf.PdfDocument pdfDoc = converter.ConvertHtmlString(htmlDoc.DocumentNode.InnerHtml); pdfDoc.Save(filePath); pdfDoc.Close(); } catch { MessageBox.Show("Printing failed. Is file open in another application?"); } }
/// <summary> /// Print method. /// </summary> public void Print(string filePath, D3jsLib.PdfStyle style) { EO.WebBrowser.WebView view = this.browser.WebView; if (view != null) { string htmlCode = view.GetHtml(); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlCode); HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='gridster-box']"); if (nodes != null) { foreach (HtmlNode n in nodes) { n.InnerHtml = ""; } // attempt to move *dep file D3jsLib.Utilities.ChartsUtilities.MoveDepFile(); // create converter SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); // set converter options SelectPdf.HtmlToPdfOptions options = converter.Options; options.PdfPageOrientation = style.Orientation; options.PdfPageSize = style.Size; options.JpegCompressionLevel = style.Compression; options.JavaScriptEnabled = true; options.EmbedFonts = true; options.KeepImagesTogether = true; options.KeepTextsTogether = true; options.AutoFitHeight = style.VerticalFit; options.AutoFitWidth = style.HorizontalFit; options.MarginTop = style.MarginTop; options.MarginRight = style.MarginRight; options.MarginBottom = style.MarginBottom; options.MarginLeft = style.MarginLeft; // created unescaped file path removes %20 from path etc. string finalFilePath = filePath; Uri uri = new Uri(filePath); string absoluteFilePath = Uri.UnescapeDataString(uri.AbsoluteUri); if (Uri.IsWellFormedUriString(absoluteFilePath, UriKind.RelativeOrAbsolute)) { Uri newUri = new Uri(absoluteFilePath); finalFilePath = newUri.LocalPath; } // convert html to document object and save SelectPdf.PdfDocument pdfDoc = converter.ConvertHtmlString(htmlDoc.DocumentNode.InnerHtml); pdfDoc.Save(finalFilePath); pdfDoc.Close(); } else { } } }