Ошибка при использовании wkhtmltopdf с Cobrebem и HTML - PullRequest
0 голосов
/ 03 октября 2019

Я использую инструмент wkhtmltopdf для генерации PDF, я провел тест с использованием URL-адреса Google "www.google.com", он отлично работал.

Теперь я сомневаюсь, я работаю с Copper, чтобысгенерировать распечатку и сгенерировать PDF, я сохраняю hmtl в строке и иду в программу wkhtmltopdf, возвращает ошибку, потому что передал строку с URL.

Нужно ли мне преобразовывать или что-то передаватьисполняемый файл?

        // get html 
        var html = _CobreBemX.GeraHTMLBoleto[0];

        htmlToPdf(@"C:\Users\eduardo.almeida\OneDrive - Solo Network Brasil S.A\Documentos\Cobrebem\ServPJ", "BOL", html);

        //Release instance CobreBemX
        _CobreBemX = null;
    }

    public static string htmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string url,
               string[] options = null,
               string pdfHtmlToPdfExePath = @"C:\Users\eduardo.almeida\OneDrive - Solo Network Brasil S.A\Documentos\Cobrebem\ServPJ\wkhtmltopdf.exe")
    {
        string urlsSeparatedBySpaces = string.Empty;
        try
        {
            //Determine inputs
            if ((url == null) || (url.Length == 0))
                throw new Exception("URL inválida");

            string outputFolder = pdfOutputLocation;
            string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".PDF"; // assemble destination PDF file name

            var p = new System.Diagnostics.Process()
            {
                StartInfo =
                {
                    FileName = pdfHtmlToPdfExePath,
                    Arguments = url + " " + outputFilename,
                    UseShellExecute = false, // needs to be false in order to redirect output
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
                    WorkingDirectory = pdfOutputLocation
                }
            };

            p.Start();

            // read the output here...
            var output = p.StandardOutput.ReadToEnd();
            var errorOutput = p.StandardError.ReadToEnd();

            // ...then wait n milliseconds for exit (as after exit, it can't read the output)
            p.WaitForExit(60000);

            // read the exit code, close process
            int returnCode = p.ExitCode;
            p.Close();

            // if 0 or 2, it worked so return path of pdf
            if ((returnCode == 0) || (returnCode == 2))
                return outputFolder + outputFilename;
            else
                throw new Exception(errorOutput);
        }
        catch (Exception exc)
        {
            throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
        }
...