Esempio n. 1
0
        public void DisableLocalFileAccess_DoesNotError(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            // create HTML file
            string html = $@"<!DOCTYPE html>
<html>
<head>
</head>
<body>
Page 1
</body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html, this.TestContext))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--disable-local-file-access \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page1 = pdfDocument.GetPage(1);
                        Assert.AreEqual("Page 1", page1.Text);
                    }
                }
            }
        }
Esempio n. 2
0
        public void PassInputsToStandardInput_ReadsArgumentsFromStandardInput(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--read-args-from-stdin";
                    string             stdIn       = $"\"{htmlFile.FilePath.Replace("\\", "\\\\")}\" \"{pdfFile.FilePath.Replace("\\", "\\\\")}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine, stdIn);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        Assert.AreEqual("Test Page", page.Text);
                    }
                }
            }
        }
Esempio n. 3
0
        public void PageOffsetTest(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
   <p style=""page-break-after: always;"">&nbsp;</p>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html, this.TestContext))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--page-offset 1 --footer-right [page] \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(2, pdfDocument.NumberOfPages, "Number of pages");
                        Page page = pdfDocument.GetPage(1);
                        IEnumerable <Word> words = page.GetWords();
                        Assert.AreEqual(3, words.Count());
                        Assert.AreEqual("Page 1", $"{words.ElementAt(0)} {words.ElementAt(1)}");
                        Assert.AreEqual("2", words.Last().Text, "Page number");
                    }
                }
            }
        }
        public void NoJavascriptDelay_ReturnsError()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string[] commandLine = new[]
                    {
                        "--log-level Error",
                        "--javascript-delay",
                        $"\"{htmlFile.FilePath}\"",
                        $"\"{pdfFile.FilePath}\"",
                    };

                    HtmlToPdfRunResult result = runner.Run(string.Join(" ", commandLine));
                    Assert.AreEqual(1, result.ExitCode);

                    string expectedOutput = HelpTextGenerator.GenerateParserError(
                        "Option 'javascript-delay' is defined with a bad format.");
                    Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
                    Assert.IsTrue(result.StandardError.Trim().StartsWith(expectedOutput.Trim()), result.StandardError);
                }
            }
        }
Esempio n. 5
0
        public void FooterFontName_WithTimesNewRoman_SetsTheFooterFontFamilyToTimesNewRoman(string exeFileName, string expectedFontName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--footer-font-name \"Times New Roman\" --footer-right [page] \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page1 = pdfDocument.GetPage(1);
                        IEnumerable <Word> words = page1.GetWords();
                        Assert.AreEqual(3, words.Count());
                        Assert.AreEqual("Page 1 1", string.Join(" ", words));

                        Assert.AreEqual(expectedFontName, $"{words.ElementAt(2).FontName}");
                    }
                }
            }
        }
        public void JavascriptDelayTest(
            string exeFileName,
            string javascriptDelayInMilliseconds,
            double expectedMinJavascriptDelayInMilliseconds)
        {
            string          htmlFilePath = "JavascriptCounter.html";
            HtmlToPdfRunner runner       = new HtmlToPdfRunner(exeFileName);

            using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
            {
                string commandLine = string.Empty;

                if (!string.IsNullOrEmpty(javascriptDelayInMilliseconds))
                {
                    commandLine = $"--javascript-delay {javascriptDelayInMilliseconds} ";
                }

                commandLine += $"\"{htmlFilePath}\" \"{pdfFile.FilePath}\"";
                HtmlToPdfRunResult result = runner.Run(commandLine);
                Assert.AreEqual(0, result.ExitCode, result.Output);

                using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                {
                    Assert.AreEqual(1, pdfDocument.NumberOfPages);
                    Page page = pdfDocument.GetPage(1);

                    int milliseconds = int.Parse(page.Text);
                    Assert.IsTrue(milliseconds >= expectedMinJavascriptDelayInMilliseconds, page.Text);
                }
            }
        }
Esempio n. 7
0
        public void HeaderFontSize_DefaultValue(string exeFileName, string expectedFontSize)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--header-right [page] \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page1 = pdfDocument.GetPage(1);
                        IEnumerable <Word> words = page1.GetWords();
                        Assert.AreEqual(3, words.Count());
                        Assert.AreEqual("1 Page 1", string.Join(" ", words));

                        Assert.AreEqual(expectedFontSize, $"{words.ElementAt(0).Letters[0].FontSize}");
                    }
                }
            }
        }
Esempio n. 8
0
        public void Encoding_WithUtf8_DoesNotError(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--encoding utf-8 \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        Assert.AreEqual("Test Page", page.Text);
                    }
                }
            }
        }
Esempio n. 9
0
        public void DashAsLastParameter_WritesPdfToStandardOutput(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"\"{htmlFile.FilePath}\" -";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    File.WriteAllText(pdfFile.FilePath, result.StandardOutput, Encoding.Default);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        Assert.AreEqual("Test Page", page.Text);
                    }
                }
            }
        }
Esempio n. 10
0
        public void WithoutTableOfContents_DoesNotInsertTableOfContentsPage(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page1 = pdfDocument.GetPage(1);
                        Assert.AreEqual("Page 1", page1.Text);
                    }
                }
            }
        }
Esempio n. 11
0
        public void UserStyleSheet_WithBodyColorBlue_MakesLettersBlue(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            string css = @"body { color: blue; }";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempCssFile cssFile = new TempCssFile(css))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"--user-style-sheet \"{cssFile.FilePath}\" \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(1, pdfDocument.NumberOfPages);
                            Page page = pdfDocument.GetPage(1);
                            Assert.IsTrue(page.Letters.All(x => (RGBColor)x.Color == new RGBColor(0, 0, 1)));
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public void DumpOutline_OutputsOutlineToFile(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            foreach (string directory in Directory.EnumerateDirectories(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "DumpOutline")))
            {
                string directoryName = new DirectoryInfo(directory).Name;

                IEnumerable <string> htmlFiles = Directory.EnumerateFiles(directory, "*.html");
                string expectedOutlineXmlFile  = Path.Combine(directory, "Outline.xml");

                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    using (TempFile tempOutlineFile = new TempFile(".xml", this.TestContext))
                    {
                        List <string> commandLineArguments = new List <string>
                        {
                            $"--dump-outline \"{tempOutlineFile.FilePath}\"",
                        };

                        commandLineArguments.AddRange(htmlFiles.Select(x => $"\"{x}\""));
                        commandLineArguments.Add($"\"{pdfFile.FilePath}\"");

                        HtmlToPdfRunResult result = runner.Run(string.Join(" ", commandLineArguments));

                        string errorMessage = $"Test: {directoryName}";

                        Assert.AreEqual(0, result.ExitCode, errorMessage + Environment.NewLine + result.Output);

                        XmlAssert.AreEqual(expectedOutlineXmlFile, tempOutlineFile.FilePath, this.TestContext, errorMessage + $" Actual: {tempOutlineFile.FileName}");
                    }
                }
            }
        }
Esempio n. 13
0
        public void HeaderRight_WithVariable_ReplacesVariableInHeader(
            string exeFileName,
            string headerCommandLineArgument,
            string expectedHeaderTextTemplate)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
    <title>The title of the test page</title>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                Dictionary <string, object> dictionary = new Dictionary <string, object>
                {
                    { "url", htmlFile.FilePath },
                };
                Hash hash = Hash.FromDictionary(dictionary);
                Template.RegisterFilter(typeof(NaturalDateFilter));
                Template template           = Template.Parse(expectedHeaderTextTemplate);
                string   expectedHeaderText = template.Render(hash);

                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--margin-top 5mm --header-right {headerCommandLineArgument} \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        IEnumerable <Word> words = page.GetWords();
                        Assert.IsTrue(words.Count() >= 3, $"{words.Count()}");
                        Assert.AreEqual("Test Page", $"{words.ElementAt(words.Count() - 2)} {words.ElementAt(words.Count() - 1)}");
                        string headerText = string.Join(" ", words.Take(words.Count() - 2).Select(x => x.Text.ToLower()));

                        if (headerCommandLineArgument == "[time]")
                        {
                            // assert times are within 5 seconds
                            DateTime expectedDateTime = DateTime.ParseExact(expectedHeaderText, "h:mm:ss tt", CultureInfo.InvariantCulture);
                            DateTime actualDateTime   = DateTime.ParseExact(headerText, "h:mm:ss tt", CultureInfo.InvariantCulture);
                            DateAssert.AreWithinFiveSeconds(expectedDateTime, actualDateTime);
                        }
                        else
                        {
                            Assert.AreEqual(expectedHeaderText.ToLower(), headerText);
                        }
                    }
                }
            }
        }
Esempio n. 14
0
        public void HelpArgument_DisplaysHelpText(string commandLine)
        {
            string expectedOutput = HelpTextGenerator.Generate();

            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            HtmlToPdfRunResult result = runner.Run(commandLine);

            Assert.AreEqual(1, result.ExitCode, result.Output);
            Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
            StringAssertWithDiff.AreEqual(expectedOutput.Trim(), result.StandardError.Trim());
        }
Esempio n. 15
0
        public void VersionArgument_DisplaysVersionInformation(string commandLine)
        {
            string expectedOutput = HelpTextGenerator.GetVersionInformation();

            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            HtmlToPdfRunResult result = runner.Run(commandLine);

            Assert.AreEqual(1, result.ExitCode, result.Output);
            Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
            Assert.IsTrue(result.StandardError.Trim().StartsWith(expectedOutput.Trim()), result.StandardError);
        }
Esempio n. 16
0
        public void HeaderRight_WithCssToHideHeaderOnFirstPage_DoesNotShowHeaderOnFirstPage()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            string html1 = @"<!DOCTYPE html>
<html>
  <head>
    <style>
        @page {margin-right: 0;}
    </style>
  </head>
  <body>
   Page 1
  </body>
</html>";

            string html2 = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile1 = new TempHtmlFile(html1, this.TestContext))
            {
                using (TempHtmlFile htmlFile2 = new TempHtmlFile(html2, this.TestContext))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"--header-right [page] \"{htmlFile1.FilePath}\" \"{htmlFile2.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(2, pdfDocument.NumberOfPages);
                            Page page1 = pdfDocument.GetPage(1);
                            IEnumerable <Word> words = page1.GetWords();
                            Assert.AreEqual(3, words.Count(), string.Join(" ", words));
                            Assert.AreEqual("1 Page 1", string.Join(" ", words));

                            Page page2 = pdfDocument.GetPage(2);
                            words = page2.GetWords();
                            Assert.AreEqual(3, words.Count());
                            Assert.AreEqual("Page 2 2", string.Join(" ", words));
                        }
                    }
                }
            }
        }
Esempio n. 17
0
        public void PageHeightAndWidthTest(
            string exeFileName,
            string height,
            string width,
            int expectedHeight,
            int expectedWidth)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string commandLine = string.Empty;

                    if (!string.IsNullOrEmpty(height))
                    {
                        commandLine += $"--page-height {height} ";
                    }

                    if (!string.IsNullOrEmpty(width))
                    {
                        commandLine += $"--page-width {width} ";
                    }

                    commandLine += $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);

                        string message = $"Size: {page.Size} Height: {page.Height} Width: {page.Width}";
                        Assert.AreEqual(expectedWidth, page.Width, message);
                        Assert.AreEqual(expectedHeight, page.Height, message);
                    }
                }
            }
        }
Esempio n. 18
0
        public void NoArguments_DisplaysErrorAndHelpText()
        {
            string expectedOutput = "System.ApplicationException: At least one input must be specified.";

            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            HtmlToPdfRunResult result = runner.Run("--log-level Error");

            Assert.AreEqual(1, result.ExitCode, result.Output);
            Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
            int length = expectedOutput.Trim().Length;

            StringAssertWithDiff.AreEqual(expectedOutput.Trim(), result.StandardError.Trim().Substring(0, length));
        }
Esempio n. 19
0
        public void BackgroundTest(string exeFileName, string commandLineParameter, bool expectedBackground)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body style=""background-color:blue;"">
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string commandLine = string.Empty;

                    if (!string.IsNullOrEmpty(commandLineParameter))
                    {
                        commandLine += $"{commandLineParameter} ";
                    }

                    commandLine += $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (PdfReader pdfReader = new PdfReader(pdfFile.FilePath))
                    {
                        using (PdfDocument pdfDocument = new PdfDocument(pdfReader))
                        {
                            int pageCount = pdfDocument.GetNumberOfPages();
                            Assert.AreEqual(1, pageCount);

                            PdfPage page = pdfDocument.GetPage(1);

                            RectangleFinder rectangleFinder = new RectangleFinder();

                            PdfCanvasProcessor processor = new PdfCanvasProcessor(rectangleFinder);
                            processor.ProcessPageContent(page);

                            ICollection <Rectangle> boxes = rectangleFinder.GetBoundingBoxes();

                            Assert.AreEqual(expectedBackground ? 1 : 0, boxes.Count());
                        }
                    }
                }
            }
        }
Esempio n. 20
0
        public void Header_WithPage_InsertsPageNumberInHeaderInMultiplePages(string exeFileName, string headerCommandLineArgument)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html1 = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            string html2 = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile1 = new TempHtmlFile(html1))
            {
                using (TempHtmlFile htmlFile2 = new TempHtmlFile(html2))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"--margin-top 5mm {headerCommandLineArgument} [page] \"{htmlFile1.FilePath}\" \"{htmlFile2.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(2, pdfDocument.NumberOfPages);
                            Page page1 = pdfDocument.GetPage(1);
                            IEnumerable <Word> words = page1.GetWords();
                            Assert.AreEqual(3, words.Count(), string.Join(" ", words));
                            Assert.AreEqual("1 Page 1", string.Join(" ", words));

                            Page page2 = pdfDocument.GetPage(2);
                            words = page2.GetWords();
                            Assert.AreEqual(3, words.Count());
                            Assert.AreEqual("2 Page 2", string.Join(" ", words));
                        }
                    }
                }
            }
        }
Esempio n. 21
0
        public void MultipleInputs_Succeeds()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            string html1 = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            string html2 = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile1 = new TempHtmlFile(html1))
            {
                using (TempHtmlFile htmlFile2 = new TempHtmlFile(html2))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"\"{htmlFile1.FilePath}\" \"{htmlFile2.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(2, pdfDocument.NumberOfPages);
                            Page page1 = pdfDocument.GetPage(1);
                            IEnumerable <Word> words = page1.GetWords();
                            Assert.AreEqual(2, words.Count());
                            Assert.AreEqual("Page 1", $"{words.ElementAt(0)} {words.ElementAt(1)}");

                            Page page2 = pdfDocument.GetPage(2);
                            words = page2.GetWords();
                            Assert.AreEqual(2, words.Count());
                            Assert.AreEqual("Page 2", $"{words.ElementAt(0)} {words.ElementAt(1)}");
                        }
                    }
                }
            }
        }
Esempio n. 22
0
        public void OrientationTest(
            string exeFileName,
            string orientation,
            PageSize pageSize,
            int width,
            int height)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string commandLine = string.Empty;

                    if (!string.IsNullOrEmpty(orientation))
                    {
                        commandLine += $"--orientation {orientation} ";
                    }

                    commandLine += $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        Assert.AreEqual(pageSize, page.Size);
                        Assert.AreEqual(width, page.Width);
                        Assert.AreEqual(height, page.Height);
                        Assert.AreEqual(0, page.Rotation.Radians);
                        Assert.AreEqual(0, page.Rotation.Value);
                    }
                }
            }
        }
Esempio n. 23
0
        public void FooterHtml_WithUrl_SetsTheFooterWithContentsOfUrl(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 1
  </body>
</html>";

            string footerHtml = @"<div>New Footer HTML</div>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (var tempDirectory = new TempDirectory())
                {
                    string footerHtmlFileName = "footer.html";
                    string footerHtmlFilePath = Path.Combine(tempDirectory.DirectoryPath, footerHtmlFileName);
                    File.WriteAllText(footerHtmlFilePath, footerHtml);

                    using (var server = new TestWebServer("/", tempDirectory.DirectoryPath))
                    {
                        string footerHtmlUrl = $"{server.RootUrl}{footerHtmlFileName}";
                        using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                        {
                            string             commandLine = $"--footer-html \"{footerHtmlUrl}\" \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                            HtmlToPdfRunResult result      = runner.Run(commandLine);
                            Assert.AreEqual(0, result.ExitCode, result.Output);

                            using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                            {
                                Assert.AreEqual(1, pdfDocument.NumberOfPages);
                                Page page1 = pdfDocument.GetPage(1);
                                IEnumerable <Word> words = page1.GetWords();
                                Assert.AreEqual("Page 1", $"{words.ElementAt(0)} {words.ElementAt(1)}");

                                Assert.AreEqual("New Footer HTML", string.Join(" ", words.Skip(2).Select(x => x.Text)));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 24
0
        public void Cover_WithHeaderOrFooter_DoesNotApplyToCoverPage(string exeFileName, string headerOrFooterCommandLineArgument, string expectedText)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string cover = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Cover Page
  </body>
</html>";

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile coverFile = new TempHtmlFile(cover))
            {
                using (TempHtmlFile htmlFile = new TempHtmlFile(html))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"{headerOrFooterCommandLineArgument} cover \"{coverFile.FilePath}\" \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(2, pdfDocument.NumberOfPages);
                            Page coverPage = pdfDocument.GetPage(1);
                            Assert.AreEqual("Cover Page", coverPage.Text);
                            Page page2 = pdfDocument.GetPage(2);
                            IEnumerable <Word> words = page2.GetWords();
                            Assert.AreEqual(3, words.Count());
                            Assert.AreEqual(expectedText, string.Join(" ", words));
                        }
                    }
                }
            }
        }
Esempio n. 25
0
        public void Cover_WithHtmlFile_InsertsCoverAsFirstPage(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string cover = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Cover Page
  </body>
</html>";

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile coverFile = new TempHtmlFile(cover, this.TestContext))
            {
                using (TempHtmlFile htmlFile = new TempHtmlFile(html, this.TestContext))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"cover \"{coverFile.FilePath}\" \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                        {
                            Assert.AreEqual(2, pdfDocument.NumberOfPages);
                            Page coverPage = pdfDocument.GetPage(1);
                            Assert.AreEqual("Cover Page", coverPage.Text);
                            Page page2 = pdfDocument.GetPage(2);
                            Assert.AreEqual("Page 2", page2.Text);
                        }
                    }
                }
            }
        }
Esempio n. 26
0
        public void FooterRight_WithCssToHideFooterOnFirstPageOfMultiplePages_DoesNotShowFooterOnFirstPage()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            string html = @"<!DOCTYPE html>
<html>
  <head>
    <style>
        @page:first {margin-right: 0;}
    </style>
  </head>
  <body>
   Page 1
   <p style=""page-break-before: always""/>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html, this.TestContext))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--footer-right [page] \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(2, pdfDocument.NumberOfPages);
                        Page page1 = pdfDocument.GetPage(1);
                        IEnumerable <Word> words = page1.GetWords();
                        Assert.AreEqual(3, words.Count(), string.Join(" ", words.Select(x => x.Text)));
                        Assert.AreEqual("Page 1 1", string.Join(" ", words));

                        Page page2 = pdfDocument.GetPage(2);
                        words = page2.GetWords();
                        Assert.AreEqual(3, words.Count(), string.Join(" ", words.Select(x => x.Text)));
                        Assert.AreEqual("Page 2 2", string.Join(" ", words));
                    }
                }
            }
        }
Esempio n. 27
0
        public void Input_WithFileDoesNotExist_ReturnsAnError()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            using (TempHtmlFile htmlFile = new TempHtmlFile())
            {
                File.Delete(htmlFile.FilePath);

                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--log-level Error \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(1, result.ExitCode);

                    string expectedOutput = "System.IO.FileNotFoundException: File not found: ";
                    Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
                    Assert.IsTrue(result.StandardError.Trim().StartsWith(expectedOutput.Trim()), result.StandardError);
                }
            }
        }
Esempio n. 28
0
        public void Outline_DoesNotError(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   <h1>Test Page</h1>
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string             commandLine = $"--outline \"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result      = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);

                        bool containsBookmarks = pdfDocument.TryGetBookmarks(out Bookmarks bookmarks);
                        Assert.IsTrue(containsBookmarks);

                        Assert.AreEqual(1, bookmarks.Roots.Count);
                        BookmarkNode rootBookmark = bookmarks.Roots[0];
                        Assert.AreEqual("Test Page", rootBookmark.Title);
                        Assert.AreEqual(0, rootBookmark.Level);
                        Assert.AreEqual(true, rootBookmark.IsLeaf);
                        Assert.AreEqual(0, rootBookmark.Children.Count);

                        Assert.AreEqual("Test Page", page.Text);
                    }
                }
            }
        }
Esempio n. 29
0
        public void InvalidPageSize_ReturnsError()
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe);

            string html = @"<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string[] commandLine = new[]
                    {
                        "--log-level Error",
                        "--page-size invalid",
                        $"\"{htmlFile.FilePath}\"",
                        $"\"{pdfFile.FilePath}\"",
                    };

                    HtmlToPdfRunResult result = runner.Run(string.Join(" ", commandLine));
                    Assert.AreEqual(1, result.ExitCode);

                    string[] expectedErrorMessage = new[]
                    {
                        "System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.",
                        "Parameter name: invalid",
                    };

                    string expectedOutput = string.Join(Environment.NewLine, expectedErrorMessage);
                    Assert.IsTrue(string.IsNullOrEmpty(result.StandardOutput), result.StandardOutput);
                    Assert.IsTrue(result.StandardError.Trim().StartsWith(expectedOutput.Trim()), result.StandardError);
                }
            }
        }
Esempio n. 30
0
        public void Title_SetsThePdfTitle(string exeFileName, string title, string expectedTitle)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string html = @"<!DOCTYPE html>
<html>
  <head>
    <title>Test Page Title</title>
  </head>
  <body>
   Test Page
  </body>
</html>";

            using (TempHtmlFile htmlFile = new TempHtmlFile(html))
            {
                using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                {
                    string commandLine = string.Empty;

                    if (!string.IsNullOrEmpty(title))
                    {
                        commandLine += $"--title \"{title}\" ";
                    }

                    commandLine += $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\"";
                    HtmlToPdfRunResult result = runner.Run(commandLine);
                    Assert.AreEqual(0, result.ExitCode, result.Output);

                    using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath))
                    {
                        Assert.AreEqual(1, pdfDocument.NumberOfPages);
                        Page page = pdfDocument.GetPage(1);
                        Assert.AreEqual(expectedTitle, pdfDocument.Information.Title);
                    }
                }
            }
        }