Esempio n. 1
0
        private string ExecutePhantomJs(string phantomJsExeToUse, string inputFileName, string outputFolder, PdfGeneratorParams param)
        {
            if (param == null)
            {
                param = new PdfGeneratorParams();
            }

            var layout = param.PageWidth > 0 && param.PageHeight > 0
        ? $"{param.PageWidth}{param.DimensionUnit.GetValue()}*{param.PageHeight}{param.DimensionUnit.GetValue()}"
        : param.Format.ToString();
            var outputFileName = Path.GetFileNameWithoutExtension(inputFileName);
            var outputFilePath = Path.Combine(outputFolder, $"{outputFileName}.pdf");
            var exePath        = Path.Combine(PhantomRootFolder, phantomJsExeToUse);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
                RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                SetFilePermission(phantomJsExeToUse);
            }

            var startInfo = new ProcessStartInfo
            {
                FileName         = exePath,
                WorkingDirectory = PhantomRootFolder,
                Arguments        = $@"rasterize.js ""{inputFileName}"" ""{outputFilePath}"" ""{layout}"" {param.ZoomFactor}",
                // TODO: include orientation parameter ""{param.Orientation.GetValue()}"" ",
                UseShellExecute = false,
                CreateNoWindow  = true
            };

            var proc = new Process {
                StartInfo = startInfo
            };

            proc.Start();
            proc.WaitForExit();

            return(outputFilePath);
        }
Esempio n. 2
0
        /// <summary>
        ///   This function takes in an 'html' string and generates a pdf
        ///   containing its rendered version, and writes the pdf to the passed
        ///   output 'outputFolder'.
        /// </summary>
        /// <param name="html">A string with html page contents.</param>
        /// <param name="outputFolder">
        ///   The folder where the file will be created.
        ///   <param name="param">An instance of <c>PdfGeneratorParams</c> class.</param>
        ///   If the output folder is null, empty or doesn't exist, the current app
        ///   directory will be used.
        /// </param>
        /// <returns>The absolute path to the new file created.</returns>
        public string GeneratePdf(string html, string outputFolder = null, PdfGeneratorParams param = null)
        {
            if (string.IsNullOrEmpty(outputFolder) || !Directory.Exists(outputFolder))
            {
                outputFolder = PhantomRootFolder;
            }

            string pdfFileName;
            var    htmlFileName = WriteHtmlToTempFile(html);

            try
            {
                var exeToUse = GetOsExecutableName();
                WriteResourcesToDisk(exeToUse);
                WriteResourcesToDisk(Consts.Rasterize);
                pdfFileName = ExecutePhantomJs(exeToUse, htmlFileName, outputFolder, param);
            }
            finally
            {
                File.Delete(Path.Combine(PhantomRootFolder, htmlFileName));
            }

            return(pdfFileName);
        }