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); } } } }
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); } } } }
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;""> </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); } } }
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}"); } } } }
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); } } } }
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); } } }
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); } } } }
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); } } } }
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))); } } } } }
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 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}"); } } } }
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); } } } } }
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)); } } } } }
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()); } } } } }
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); } } } }
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)}"); } } } } }
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)); } } } } }
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); } } } }
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))); } } } } } }
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)); } } } } }
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); } } } } }
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)); } } } }
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); } } }
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); } } } }
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); } } }
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); } } } }
public void HeaderHtml_WithLocalFilePath_SetsTheHeaderWithContentsOfFile(string exeFileName) { HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName); string html = @"<!DOCTYPE html> <html> <head> </head> <body> Page 1 </body> </html>"; string headerHtml = @"<div>New Header HTML</div>"; using (TempHtmlFile htmlFile = new TempHtmlFile(html)) { using (TempHtmlFile htmlHeaderFile = new TempHtmlFile(headerHtml)) { using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext)) { string commandLine = $"--header-html \"{htmlHeaderFile.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 page1 = pdfDocument.GetPage(1); IEnumerable <Word> words = page1.GetWords(); Assert.AreEqual("New Header HTML Page 1", string.Join(" ", words)); } } } } }
public void AdditionalArgumentsTest() { HtmlToPdfRunner runner = new HtmlToPdfRunner(HtmlToPdfRunner.HtmlToPdfExe); string html = @"<!DOCTYPE html> <html> <head> </head> <body> <p id=""userAgent""></p> <script> let agent = navigator.userAgent; document.getElementById(""userAgent"").innerHTML = ""User-agent:<br>"" + agent; </script> </body> </html>"; using (TempHtmlFile htmlFile = new TempHtmlFile(html)) { using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext)) { string commandLine = $"--additional-arguments=\"--user-agent=MyCustomUserAgent\" \"{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.AreEqual("User-agent: MyCustomUserAgent", string.Join(" ", words)); } } } }
public void TableOfContents_InsertsTableOfContentsAsFirstPage(string exeFileName, bool wkhtmltopdf, bool disableDottedLines) { HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName); string html = @"<!DOCTYPE html> <html> <head> </head> <body> Page 2 </body> </html>"; using (TempHtmlFile htmlFile = new TempHtmlFile(html, this.TestContext)) { using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext)) { using (TempFile tempOutlineFile = new TempFile(".xml", this.TestContext)) { string commandLine = $"--dump-outline \"{tempOutlineFile.FilePath}\" toc "; if (disableDottedLines) { commandLine += "--disable-dotted-lines "; } commandLine += $"\"{htmlFile.FilePath}\" \"{pdfFile.FilePath}\""; HtmlToPdfRunResult result = runner.Run(commandLine); Assert.AreEqual(0, result.ExitCode, result.Output); // assert outline using (TempFile expectedOutlineFile = new TempFile(".xml", this.TestContext)) { string[] contents = new[] { @"<?xml version=""1.0"" encoding=""UTF-8""?>", @"<outline xmlns=""http://wkhtmltopdf.org/outline"">", @" <item title=""Table of Contents"" page=""0"" link=""__WKANCHOR_0"" backLink=""__WKANCHOR_0"">", @" <item title=""Table of Contents"" page=""1"" link=""__WKANCHOR_2"" backLink=""__WKANCHOR_2""/>", @" </item>", @" <item title="""" page=""1"" link="""" backLink=""""/>", @"</outline>", }; File.WriteAllLines(expectedOutlineFile.FilePath, contents); XmlAssert.AreEqual(expectedOutlineFile.FilePath, tempOutlineFile.FilePath, this.TestContext, $"Expected: {expectedOutlineFile.FileName} Actual: {tempOutlineFile.FileName}"); } // assert PDF using (var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFile.FilePath)) { Assert.AreEqual(2, pdfDocument.NumberOfPages); Page tocPage = pdfDocument.GetPage(1); // assert text IEnumerable <Word> words = tocPage.GetWords(); Assert.AreEqual(7, words.Count()); Assert.AreEqual("Table", words.ElementAt(0).Text); Assert.AreEqual("of", words.ElementAt(1).Text); Assert.AreEqual("Contents", words.ElementAt(2).Text); Assert.AreEqual("Table", words.ElementAt(3).Text); Assert.AreEqual("of", words.ElementAt(4).Text); Assert.AreEqual("Contents", words.ElementAt(5).Text); Assert.AreEqual("1", words.ElementAt(6).Text); // assert link IEnumerable <Annotation> annotations = tocPage.ExperimentalAccess.GetAnnotations(); Assert.AreEqual(1, annotations.Count()); Annotation annotation = annotations.ElementAt(0); Assert.AreEqual(AnnotationType.Link, annotation.Type); string errorMessage = "Annotation dictionary:" + Environment.NewLine + string.Join( Environment.NewLine, annotation.AnnotationDictionary.Data.Select(x => $"{x.Key} {x.Value.GetType()} {x.Value}")); Assert.IsTrue(annotation.AnnotationDictionary.TryGet(NameToken.Type, out NameToken typeToken), errorMessage); Assert.AreEqual("Annot", typeToken.Data); Assert.IsTrue(annotation.AnnotationDictionary.TryGet(NameToken.Subtype, out NameToken subtypeToken), errorMessage); Assert.AreEqual("Link", subtypeToken.Data); if (wkhtmltopdf) { Assert.IsTrue(annotation.AnnotationDictionary.TryGet(NameToken.Dest, out NameToken destinationToken), errorMessage); Assert.AreEqual("__WKANCHOR_2", destinationToken.Data); } else { Assert.IsTrue(annotation.AnnotationDictionary.TryGet(NameToken.A, out IndirectReferenceToken aToken), errorMessage); Assert.AreEqual(27, aToken.Data.ObjectNumber); Assert.IsTrue(annotation.AnnotationDictionary.TryGet(NameToken.P, out IndirectReferenceToken pToken), errorMessage); Assert.AreEqual(16, pToken.Data.ObjectNumber); } // assert graphic operations IReadOnlyList <IGraphicsStateOperation> graphicsStateOperations = tocPage.Operations; errorMessage = "Graphics State Operations:" + Environment.NewLine + string.Join( Environment.NewLine, graphicsStateOperations.Select(x => $"{x.Operator} {x.GetType()} {x}")); if (wkhtmltopdf) { Assert.AreEqual(!disableDottedLines, graphicsStateOperations.Any(x => x is ModifyClippingByNonZeroWindingIntersect), errorMessage); Assert.AreEqual(!disableDottedLines, graphicsStateOperations.Any(x => x is AppendRectangle), errorMessage); Assert.AreEqual(!disableDottedLines, graphicsStateOperations.Any(x => x is FillPathNonZeroWinding), errorMessage); } else { Assert.AreEqual(!disableDottedLines, graphicsStateOperations.Any(x => x is AppendStraightLineSegment), errorMessage); Assert.AreEqual(!disableDottedLines, graphicsStateOperations.Any(x => x is BeginNewSubpath), errorMessage); } // assert page 2 Page page2 = pdfDocument.GetPage(2); Assert.AreEqual("Page 2", page2.Text); } } } } }