Пример #1
0
        /// <summary>
        /// Convert Html page at a given URL to a PDF file using open-source tool wkhtml2pdf
        /// </summary>
        // ReSharper disable once InconsistentNaming
        public static void ConvertURLToPDF(Uri url, FileInfo outputFile, PdfConversionSettings settings)
        {
            Check.RequireNotNull(settings);

            var exeInfo = new FileInfo(SitkaConfiguration.GetRequiredAppSetting(settings.AppKeyExecPath));

            Check.RequireFileExists(exeInfo);

            Check.RequireDirectoryExists(outputFile.DirectoryName);

            var commandLineArguments = settings.BuildCommandSwitches();

            commandLineArguments.Add(url.ToString());
            commandLineArguments.Add(outputFile.FullName);
            var result = ProcessUtility.ShellAndWaitImpl(exeInfo.DirectoryName, exeInfo.FullName, commandLineArguments,
                                                         true, Convert.ToInt32(settings.ExecutionTimeout.TotalMilliseconds));
            var retCode = result.ReturnCode;

            var argumentsAsString = String.Join(" ",
                                                commandLineArguments.Select(ProcessUtility.EncodeArgumentForCommandLine).ToList());
            var fullProcessAndArguments =
                $"{ProcessUtility.EncodeArgumentForCommandLine(exeInfo.FullName)} {argumentsAsString}";

            Check.Ensure(retCode == 0,
                         $"Process {exeInfo.Name} returned with exit code {retCode}, expected exit code 0.\r\nProcess Command Line:\r\n{fullProcessAndArguments}\r\nProcess Working Directory: {exeInfo.DirectoryName}\r\nStdErr and StdOut:\r\n{result.StdOutAndStdErr}");
        }
Пример #2
0
        // ReSharper disable once InconsistentNaming
        public static void ConvertURLToPDFWithHeadlessChrome(Uri url, FileInfo outputFile, HeadlessChromePdfConversionSettings headlessChromePdfConversionSettings)
        {
            // Settings should be provided
            Check.RequireNotNull(headlessChromePdfConversionSettings);

            // Output directory must exist
            Check.RequireDirectoryExists(outputFile.DirectoryName);

            var commandLineArguments = headlessChromePdfConversionSettings.BuildHeadlessChromeCommandLineArguments(url, outputFile);

            // TODO: Make configurable. NOTE **CURRENTLY EXPECTS CHROME CANARY** to use the --print-to-pdf-no-header option!!!
            FileInfo chromeExeLocation = new FileInfo(SitkaConfiguration.GetRequiredAppSetting("HeadlessGoogleChromeExecutable"));

            Logger.Info($"Looking for Google Headless Chrome at {chromeExeLocation}");

            // Headless Chrome must be installed where we expect it -- namely for the proper user for the web site
            Check.EnsureFileExists(chromeExeLocation);

            // You really do need to use a meaningful, multi-second timeout with Headless Chrome, otherwise you
            // won't get any PDF file written to disk.
            int          maxTimeoutMs = (int)headlessChromePdfConversionSettings.ExecutionTimeout.TotalMilliseconds;
            const string outputThatIndicatesPdfHasBeenWritten = "Written to file";

            ProcessUtility.RunCommandLineLaunchingFromCmdExeWithOptionalTimeout(outputFile.DirectoryName, chromeExeLocation, commandLineArguments, outputThatIndicatesPdfHasBeenWritten, maxTimeoutMs);
        }
Пример #3
0
        /// <summary>
        /// Convert Html page at a given URL to a PDF file using open-source tool wkhtml2pdf
        /// </summary>
        // ReSharper disable once InconsistentNaming
        public static byte[] ConvertURLToByteArray(Uri url, PdfConversionSettings settings)
        {
            var tempFolder = new DirectoryInfo(SitkaConfiguration.GetRequiredAppSetting("TempFolder"));

            Check.RequireDirectoryExists(tempFolder);

            var tempFile = new FileInfo($"{tempFolder.FullName}\\{Guid.NewGuid()}.pdf");

            ConvertURLToPDF(url, tempFile, settings);

            using (var fs = new FileStream(tempFile.FullName, FileMode.Open, FileAccess.Read))
            {
                var result = new byte[fs.Length];
                fs.Read(result, 0, result.Length);
                fs.Close();
                tempFile.Delete();
                return(result);
            }
        }