Skip to content
Loading
  • +2

    You can take the HTML page and convert it to PDF through a backend service using the DocumentConverter. Here is an example you can leverage: 

    C#
    using (DocumentConverter documentConverter = new DocumentConverter())         
    {         
       var input = @"C:\Inputfiles\page.html");        
       var output = @"C:\Outputfiles\output.pdf");         
       var jobData = DocumentConverterJobs.CreateJobData(input, output, DocumentFormat.Pdf);         
       jobData.JobName = "Convert to Pdf";         
            
       var documentWriter = new DocumentWriter();         
       documentConverter.SetDocumentWriterInstance(documentWriter);         
            
       var renderingEngine = new AnnWinFormsRenderingEngine();         
       documentConverter.SetAnnRenderingEngineInstance(renderingEngine);         
            
       var job = documentConverter.Jobs.CreateJob(jobData);         
       documentConverter.Jobs.RunJob(job);         
    }


    https://www.leadtools.com/help/sdk/tutorials/dotnet-core-convert-files-with-the-document-converter.html
    https://www.leadtools.com/help/sdk/tutorials/dotnet-core-configure-and-run-the-document-service.html

    +2
  • Mukesh Nailwal
    +2
    1. Download and install wkhtmltopdf.
    2. Use the below code.
    C#
    public static string HtmlToPdf(string outputFilenamePrefix, string[] urls,
        string[] options = null,
        string pdfHtmlToPdfExePath = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
    {
        string urlsSeparatedBySpaces = string.Empty;
        try
        {
            //Determine inputs
            if ((urls == null) || (urls.Length == 0))
                throw new Exception("No input URLs provided for HtmlToPdf");
            else
                urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs
    
            string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name
    
            var p = new System.Diagnostics.Process()
            {
                StartInfo =
                {
                    FileName = pdfHtmlToPdfExePath,
                    Arguments = ((options == null) ? "" : string.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + 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 = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
                }
            };
    
            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 outputFilename;
            else
                throw new Exception(errorOutput);
        }
        catch (Exception exc)
        {
            throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
        }
    }
    1. And call the above method as HtmlToPdf("test", new string[] { "https://www.google.com" }, new string[] { "-s A5" });
    2. If you need to convert HTML string to PDF, the tweak the above method and replace the Arguments to Process StartInfo as $@"/C echo | set /p=""{htmlText}"" | ""{pdfHtmlToPdfExePath}"" {((options == null) ? "" : string.Join(" ", options))} - ""C:\Users\xxxx\Desktop\{outputFilename}""";
    +2