Exemple #1
0
        public void GeneratePdfCommonSettingsTest()
        {
            var gen = new HtmlToPdfGeneration()
            {
                DisplayPdfAfterGeneration = true,
                FooterText            = "Blog Post - [page] of [topage]",
                Title                 = "My great document",
                ShowFooterPageNumbers = false
            };

            string file = GeneratedHtmlFile;

            Assert.IsTrue(File.Exists(file), "Input File doesn't exist");
            Console.WriteLine(file + " " + File.Exists(file));

            gen.ExecutionPath = WkhtopExecutionPath;

            Assert.IsTrue(File.Exists(gen.ExecutionPath + "wkhtmltopdf.exe"));

            bool result = gen.GeneratePdfFromHtml(file, OutputPdfFile);

            Console.WriteLine("Execution Command: " + gen.FullExecutionCommand);
            Console.WriteLine("Output Generated: " + gen.ExecutionOutputText);

            Assert.IsTrue(result, "Result should be true: " + gen.ErrorMessage);
        }
Exemple #2
0
        public void GeneratePdfWithMarginsTest()
        {
            var gen = new HtmlToPdfGeneration()
            {
                DisplayPdfAfterGeneration = true,
                ShowFooterPageNumbers     = false
            };

            gen.Margins.MarginLeft  = 50;
            gen.Margins.MarginRight = 50;



            string file = GeneratedHtmlFile;

            Assert.IsTrue(File.Exists(file), "Input File doesn't exist");
            Console.WriteLine(file + " " + File.Exists(file));



            Assert.IsTrue(File.Exists(gen.ExecutionPath + "wkhtmltopdf.exe"));

            bool result = gen.GeneratePdfFromHtml(file, OutputPdfFile);


            Console.WriteLine("Execution Command: " + gen.FullExecutionCommand);
            Console.WriteLine("Output Generated: " + gen.ExecutionOutputText);


            Assert.IsTrue(result, "Result should be true: " + gen.ErrorMessage);
        }
Exemple #3
0
        public void GeneratePdfDefaultSettingsTest()
        {
            var gen = new HtmlToPdfGeneration()
            {
                DisplayPdfAfterGeneration = false
            };

            string file = GeneratedHtmlFile;

            Assert.IsTrue(File.Exists(file), "Input File doesn't exist");
            Console.WriteLine(file + " " + File.Exists(file));

            gen.ExecutionPath = WkhtopExecutionPath;
            Assert.IsTrue(File.Exists(gen.ExecutionPath + "wkhtmltopdf.exe"));

            bool result = gen.GeneratePdfFromHtml(file, OutputPdfFile);

            Console.WriteLine("Execution Command: " + gen.FullExecutionCommand);
            Console.WriteLine("Output Generated: " + gen.ExecutionOutputText);

            Assert.IsTrue(result, "Result should be true: " + gen.ErrorMessage);
        }
Exemple #4
0
        /// <summary>
        /// mm markdowntopdf [inputfile] [outputFile] -open
        /// </summary>
        public void MarkdownToPdf()
        {
            Processor.ConsoleHeader();

            string inputFile  = Arguments.InputFile;
            string outputFile = Arguments.OutputFile;

            if (string.IsNullOrEmpty(inputFile) || !File.Exists(inputFile))
            {
                var fd = new System.Windows.Forms.OpenFileDialog
                {
                    DefaultExt = ".md",
                    Filter     = "HTML files (*.md, *.markdown, *.mdcrypt)|*.md;*.markdown,*.mdcrypt|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Title            = "Open Markdown File",
                    InitialDirectory = Environment.CurrentDirectory
                };
                var res = fd.ShowDialog();
                if (res == System.Windows.Forms.DialogResult.Cancel || res == System.Windows.Forms.DialogResult.Abort)
                {
                    return;
                }
                inputFile = fd.FileName;
            }

            if (string.IsNullOrEmpty(outputFile) || !File.Exists(outputFile))
            {
                var fd = new System.Windows.Forms.SaveFileDialog
                {
                    DefaultExt = ".pdf",
                    Filter     = "Pdf files (*.pdf)|*.pdf|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = false,
                    RestoreDirectory = true,
                    Title            = "Save as Pdf File",
                    InitialDirectory = Path.GetDirectoryName(inputFile),
                    FileName         = Path.ChangeExtension(Path.GetFileName(inputFile), "pdf")
                };
                var res = fd.ShowDialog();
                if (res == DialogResult.Cancel || res == DialogResult.Abort)
                {
                    return;
                }
                outputFile = fd.FileName;
            }

            var doc = new MarkdownDocument();

            if (!string.IsNullOrEmpty(Arguments.Theme))
            {
                mmApp.Configuration.PreviewTheme = Arguments.Theme;
            }

            try
            {
                if (!doc.Load(inputFile))
                {
                    throw new AccessViolationException();
                }
            }
            catch
            {
                ConsoleHelper.WriteError("Failed: Couldn't read input file.");
                Processor.ConsoleFooter();
                return;
            }

            string htmlFilename = System.IO.Path.ChangeExtension(doc.Filename, "html");
            var    pdfGenerator = new HtmlToPdfGeneration();

            pdfGenerator.PageSize    = Arguments.PageSize;
            pdfGenerator.Orientation = Arguments.Orientation;
            pdfGenerator.DisplayPdfAfterGeneration = Arguments.OpenOutputFile;

            try
            {
                // render the document with template and return only as string (no output yet)
                doc.RenderHtmlToFile(filename: htmlFilename, removeBaseTag: true);

                if (!pdfGenerator.GeneratePdfFromHtml(htmlFilename, outputFile))
                {
                    throw new InvalidOperationException(pdfGenerator.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteError("Failed: PDF output generation failed: " + ex.Message);
                Processor.ConsoleFooter();
                return;
            }
            finally
            {
                File.Delete(htmlFilename);
            }

            ConsoleHelper.WriteSuccess("PDF file generated: " + outputFile);
            Processor.ConsoleFooter();
        }