Sicos1977 / ChromiumHtmlToPdf

Convert HTML to PDF with a Chromium based browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"file" or URL can be converted?

ardeshir opened this issue · comments

Would like to walk down a directory of folders htm files? is this feature not possible?

  • error CS1501: No overload for method 'ConvertToPdf' takes 2 arguments [/Users/ardeshir/cargill/CDS-AI-Customer-Service/data/Help/Content/Html2PDF/Html2PDF.csproj
using System;  
using System.IO;  
using System.Linq;
using ChromiumHtmlToPdfLib;

// Get your directory  
string directoryPath = ".";  
  
string[] subdirectoryEntries = Directory.GetDirectories(directoryPath);  
  
foreach(string subdirectory in subdirectoryEntries)  
{  
    LoadSubDirs(subdirectory);  
}  
  
void LoadSubDirs(string dir)  
{  
    Console.WriteLine(dir);  
  
    string[] files = Directory.GetFiles(dir);  
  
    foreach (string file in files)  
    {  
        if(Path.GetExtension(file) == ".htm")  
        {  
            using (var converter = new Converter())  
            {  
                string pdfPath = Path.ChangeExtension(file, ".pdf");  
                converter.ConvertToPdf(new Uri(file), pdfPath);  
  
                // Show the PDF  
                System.Diagnostics.Process.Start(pdfPath);  
            }  
        }  
    }  
  
    string[] subdirectoryEntries = Directory.GetDirectories(dir);  
  
    foreach(string subdirectory in subdirectoryEntries)  
    {  
        LoadSubDirs(subdirectory);  
    }  
}  

Sure you can but you must fill in all the needed parameters

    /// <summary>
    ///     Converts the given <paramref name="inputUri" /> to PDF
    /// </summary>
    /// <param name="inputUri">The webpage to convert</param>
    /// <param name="outputStream">The output stream</param>
    /// <param name="pageSettings">
    ///     <see cref="PageSettings" />
    /// </param>
    /// <param name="waitForWindowStatus">
    ///     Wait until the javascript window.status has this value before
    ///     rendering the PDF
    /// </param>
    /// <param name="waitForWindowsStatusTimeout"></param>
    /// <param name="conversionTimeout">
    ///     An conversion timeout in milliseconds, if the conversion fails
    ///     to finished in the set amount of time then an <see cref="ConversionTimedOutException" /> is raised
    /// </param>
    /// <param name="mediaLoadTimeout">
    ///     When set a timeout will be started after the DomContentLoaded
    ///     event has fired. After a timeout the NavigateTo method will exit as if the page has been completely loaded
    /// </param>
    /// <param name="logger">
    ///     When set then this will give a logging for each conversion. Use the logger
    ///     option in the constructor if you want one log for all conversions
    /// </param>
    /// <exception cref="ConversionTimedOutException">
    ///     Raised when <paramref name="conversionTimeout" /> is set and the
    ///     conversion fails to finish in this amount of time
    /// </exception>
    /// <remarks>
    ///     When the property <see cref="CaptureSnapshot" /> has been set then the snapshot is saved to the
    ///     property <see cref="SnapshotStream" />
    /// </remarks>
    public void ConvertToPdf(
        ConvertUri inputUri,
        Stream outputStream,
        PageSettings pageSettings,
        string waitForWindowStatus = "",
        int waitForWindowsStatusTimeout = 60000,
        int? conversionTimeout = null,
        int? mediaLoadTimeout = null,
        ILogger logger = null)
    {
        ConvertToPdfAsync(
            inputUri, 
            outputStream, 
            pageSettings, 
            waitForWindowStatus, 
            waitForWindowsStatusTimeout,
            conversionTimeout,
            mediaLoadTimeout,
            logger).GetAwaiter().GetResult();
    }