コード例 #1
0
        /// <summary>
        /// Elaborate
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public String Run(String tenantId, String jobId)
        {
            Logger.DebugFormat("Converting {0} to pdf", jobId);
            var localFileName = DownloadLocalCopy(tenantId, jobId);
            var outputFileName = localFileName + ".pdf";
            var uri = new Uri(localFileName);
            
            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceOutline,
                    PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
                    Margins =
                    {
                        All = 1.375,
                        Unit = Unit.Centimeters
                    },
                    OutputFormat = GlobalSettings.DocumentOutputFormat.PDF
                },
                Objects = {
                    new ObjectSettings
                    {
                        PageUrl = uri.AbsoluteUri,
                        WebSettings = new WebSettings()
                        {
                            EnableJavascript = false,
                            PrintMediaType = false
                        }
                    },
                }
            };
            try
            {
                var converter = Factory.Create();
                var pdf = converter.Convert(document);

                File.WriteAllBytes(outputFileName, pdf);

                Logger.DebugFormat("Deleting {0}", localFileName);
                File.Delete(localFileName);
                Logger.DebugFormat("Conversion of {0} to pdf done!", jobId);

                return outputFileName;
            }
            catch (Exception ex)
            {
                
                throw;
            }
          
        }
コード例 #2
0
ファイル: Proxy.cs プロジェクト: wislon/TuesPechkin
 public byte[] Convert(HtmlToPdfDocument document)
 {
     return(SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(document)));
 }
コード例 #3
0
        /// <summary>
        /// Elaborate
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public String Run(String tenantId, String jobId)
        {
            Logger.DebugFormat("Converting {0} to pdf", jobId);
            var localFileName = DownloadLocalCopy(tenantId, jobId);
            var outputFileName = localFileName + ".pdf";
            var uri = new Uri(localFileName);
            
            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceOutline,
                    PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
                    Margins =
                    {
                        All = 1.375,
                        Unit = Unit.Centimeters
                    },
                    OutputFormat = GlobalSettings.DocumentOutputFormat.PDF
                },
                Objects = {
                    new ObjectSettings
                    {
                        PageUrl = uri.AbsoluteUri,
                        WebSettings = new WebSettings()
                        {
                            EnableJavascript = false,
                            PrintMediaType = false
                        }
                    },
                }
            };
            try
            {
                //This is the thread safe converter
                //it seems sometimes to hang forever during tests.
                //https://github.com/tuespetre/TuesPechkin
                //IConverter converter =
                //     new ThreadSafeConverter(
                //         new PdfToolset(
                //             new Win32EmbeddedDeployment(
                //                 new TempFolderDeployment())));

                //Standard, non thread safe converter.
                IConverter converter =
                    new StandardConverter(
                        new PdfToolset(
                            new Win32EmbeddedDeployment(
                                new TempFolderDeployment())));

                var pdf = converter.Convert(document);

                File.WriteAllBytes(outputFileName, pdf);

                Logger.DebugFormat("Deleting {0}", localFileName);
                File.Delete(localFileName);
                Logger.DebugFormat("Conversion of {0} to pdf done!", jobId);

                return outputFileName;
            }
            catch (Exception ex)
            {
                
                throw;
            }
          
        }
コード例 #4
0
 /// <summary>
 /// Converts external HTML resource into PDF.
 /// </summary>
 /// <param name="doc">document parameters, <code>ObjectSettings.SetPageUri</code> should be set</param>
 /// <returns>PDF document body</returns>
 public byte[] Convert(ObjectSettings settings)
 {
     var doc = new HtmlToPdfDocument();
     doc.Objects.Add(settings);
     return this.Convert(doc);
 }
コード例 #5
0
ファイル: Proxy.cs プロジェクト: JamesPatison/TuesPechkin
 public byte[] Convert(HtmlToPdfDocument document)
 {
     return SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(document));
 }
コード例 #6
0
        /// <summary>
        /// Runs conversion process.
        /// 
        /// Allows to convert both external HTML resource and HTML string.
        /// 
        /// Takes html source as a byte array for when you don't know the encoding.
        /// </summary>
        /// <param name="doc">document parameters</param>
        /// <param name="html">document body, ignored if <code>ObjectSettings.SetPageUri</code> is set</param>
        /// <returns>PDF document body</returns>
        public byte[] Convert(HtmlToPdfDocument document)
        {
            document.ApplyToConverter(out this.converter);

            Tracer.Trace(string.Format("T:{0} Created converter", Thread.CurrentThread.Name));

            PechkinStatic.SetErrorCallback(this.converter, this.onErrorDelegate);
            PechkinStatic.SetWarningCallback(this.converter, this.onWarningDelegate);
            PechkinStatic.SetPhaseChangedCallback(this.converter, this.onPhaseChangedDelegate);
            PechkinStatic.SetProgressChangedCallback(this.converter, this.onProgressChangedDelegate);
            PechkinStatic.SetFinishedCallback(this.converter, this.onFinishedDelegate);

            Tracer.Trace(string.Format("T:{0} Added callbacks to converter", Thread.CurrentThread.Name));

            // run OnBegin
            this.OnBegin(this.converter);

            // run conversion process
            if (!PechkinStatic.PerformConversion(converter))
            {
                Tracer.Trace(string.Format("T:{0} Conversion failed, null returned", Thread.CurrentThread.Name));

                return null;
            }

            // get output
            var result = PechkinStatic.GetConverterResult(this.converter);

            Tracer.Trace(string.Format("T:{0} Releasing unmanaged converter", Thread.CurrentThread.Name));

            PechkinStatic.DestroyConverter(this.converter);

            return result;
        }
コード例 #7
0
        /// <summary>
        /// Convert a url to a pdf
        /// </summary>
        /// <param name="ConverterToUse">converter to use. Use TuesConverter.PdfConverter</param>
        /// <param name="ProduceTableOfContentsOutline">Product the table of contents outline</param>
        /// <param name="PaperSize">Paper Size</param>
        /// <param name="WhatToRun">What to run. Either Html or a url</param>
        /// <param name="ModeToRun">What mode is the What To Run In.</param>
        /// <param name="HtmlToConvert">Html to convert</param>
        /// <param name="MarginTop">Margin Top</param>
        /// <param name="MarginRight">Margin Right</param>
        /// <param name="MarginBottom">Margin Bottom</param>
        /// <param name="MarginLeft">Margin Left</param>
        /// <param name="UsePrintMediaCssSelectors">When running do you want to run under the css 3 print media selectors</param>
        /// <returns>pdf file byte array</returns>
        public static byte[] ConvertAUrlToPdf(IConverter ConverterToUse,
                                              bool ProduceTableOfContentsOutline,
                                              System.Drawing.Printing.PaperKind PaperSize,
                                              IWhatToRunParameter WhatToRun,
                                              double? MarginTop,
                                              double? MarginRight,
                                              double? MarginBottom,
                                              double? MarginLeft,
                                              bool UsePrintMediaCssSelectors)
        {
            //let's build up the object settings
            var ObjectSettingsToUse = WhatToRun.ToObjectSettings();

            //add anything else now
            ObjectSettingsToUse.WebSettings = new WebSettings { PrintMediaType = UsePrintMediaCssSelectors };

            //go build the main settings to use
            var DocumentSettings = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceTableOfContentsOutline,
                    PaperSize = PaperSize,
                    Margins =
                    {
                        Top = MarginTop,
                        Right = MarginRight,
                        Bottom = MarginBottom,
                        Left = MarginLeft,
                        Unit = Unit.Inches
                    }
                },
                Objects =
                {
                    ObjectSettingsToUse
                }
            };

            //go convert and return it
            return ConverterToUse.Convert(DocumentSettings);
        }
コード例 #8
0
ファイル: FileHelper.cs プロジェクト: BoccaDamian/bubis
        internal static byte[] Convert(string html, string reportName)
        {
            Ensure();

            ObjectSettings oc = createObj(html);
            oc.HeaderSettings.LeftText = reportName;
            oc.HeaderSettings.RightText = System.DateTime.Now.ToString("dd/MM/yyyy");
            oc.FooterSettings.RightText = "Page [page] to [topage]";

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings = _globalSettings,
                Objects = { oc }
            };

            return _converter.Convert(doc);
        }
コード例 #9
0
        public static byte[] ConvertToPdf(PDFHelper.DataType type, string data, string documentName)
        {
            var webSettings = new WebSettings
            {
                EnableJavascript = true,
                LoadImages = true,
                EnableIntelligentShrinking = true
            };

            ObjectSettings objSettings = null;

            if (type == DataType.HTML)
                objSettings = new ObjectSettings { HtmlText = data, WebSettings = webSettings };
            else
                objSettings = new ObjectSettings { PageUrl = data, WebSettings = webSettings };

            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = true,
                    DocumentTitle = documentName,
                    PaperSize = PaperKind.Letter,
                    UseCompression = true,
                    ImageDPI = 600,
                    ImageQuality = 100,


                    //DPI = 1200,
                    Margins =
                    {
                        Top = 1,
                        Right = 0.5,
                        Bottom = 1,
                        Left = 0.5,
                        Unit = Unit.Centimeters
                    }
                },
                Objects = {
                   objSettings
                }
            };

            return converter.Convert(document);
        }
コード例 #10
-1
ファイル: TuesPich.cs プロジェクト: shajumohamed/html2pdf
        public byte[] convertHTMLTOPDF(string html)
        {
            var document = new HtmlToPdfDocument
             {
             GlobalSettings =
             {
             ProduceOutline = false,
             DocumentTitle = "Pretty Websites",
             PaperSize = PaperKind.Letter, // Implicit conversion to PechkinPaperSize
             Margins =
             {
             All = 1.375,
             Unit = Unit.Centimeters
             }

             },
             Objects = {
            //new ObjectSettings { HtmlText = "<h1>Pretty Websites</h1><p>This might take a bit to convert!</p>" },
            new ObjectSettings { HtmlText = html},
            //new ObjectSettings { PageUrl = "www.microsoft.com" },
            //new ObjectSettings { PageUrl = "www.github.com" }
            }
             };

            byte[] result = converter.Convert(document);
            return result;
               // ByteArrayToFile(result, "C:/temp/exmple.pdf");
        }
コード例 #11
-1
ファイル: MainForm.cs プロジェクト: JamesPatison/TuesPechkin
        private void OnConvertButtonClick(object sender, EventArgs e)
        {
            PerformanceCollector pc = new PerformanceCollector("PDF creation");

            var document = new HtmlToPdfDocument
            {
                GlobalSettings = {
                    ProduceOutline = true,
                    DocumentTitle = "Business Document",
                    PaperSize = PaperKind.A4,
                    Margins = {
                        Top = 1.5,
                        Right = 1,
                        Bottom = 1,
                        Left = 1.25,
                        Unit = Unit.Centimeters
                    }
                },
                Objects = {
                    new ObjectSettings { HtmlText = this.htmlText.Text }
                }
            };

            IPechkin sc2 = Factory.Create();
            var buf = sc2.Convert(document);

            MessageBox.Show("All conversions done");

            pc.FinishAction("conversion finished");

            if (buf == null)
            {
                MessageBox.Show("Error converting!");

                return;
            }

            try
            {
                string fn = string.Format("{0}.pdf", Path.GetTempFileName());

                FileStream fs = new FileStream(fn, FileMode.Create);
                fs.Write(buf, 0, buf.Length);
                fs.Close();

                pc.FinishAction("dumped file to disk");

                Process myProcess = new Process();
                myProcess.StartInfo.FileName = fn;
                myProcess.Start();

                pc.FinishAction("opened it");
            }
            catch
            {
            }

            pc.ShowInMessageBox(null);
        }