我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。

有没有更好的办法?


当前回答

很可能大多数项目将包装C/ c++引擎,而不是从头开始实现c#解决方案。试试哥德堡计划。

为了测试它

docker run --rm -p 3000:3000 thecodingmachine/gotenberg:6

旋度样本

curl --request POST \
    --url http://localhost:3000/convert/url \
    --header 'Content-Type: multipart/form-data' \
    --form remoteURL=https://brave.com \
    --form marginTop=0 \
    --form marginBottom=0 \
    --form marginLeft=0 \
    --form marginRight=0 \
    -o result.pdf

c# sample.cs

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
using static System.Console;

namespace Gotenberg
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            try
            {
                var client = new HttpClient();            
                var formContent = new MultipartFormDataContent
                    {
                        {new StringContent("https://brave.com/"), "remoteURL"},
                        {new StringContent("0"), "marginTop" }
                    };
                var result = await client.PostAsync(new Uri("http://localhost:3000/convert/url"), formContent);
                await File.WriteAllBytesAsync("brave.com.pdf", await result.Content.ReadAsByteArrayAsync());
            }
            catch (Exception ex)
            {
                WriteLine(ex);
            }
        }
    }
}

要编译

csc sample.cs -langversion:latest -reference:System.Net.Http.dll && mono ./sample.exe

其他回答

这取决于您的其他需求。

一个非常简单但不容易部署的解决方案是使用WebBrowser控件加载Html,然后使用Print方法打印到本地安装的PDF打印机。有一些免费的PDF打印机,WebBrowser控件是. net框架的一部分。

编辑: 如果你的Html是XHtml,你可以使用PDFizer来完成这项工作。

在c#中转换HTML到PDF使用ABCpdf。

ABCpdf可以使用Gecko或Trident呈现引擎,因此您的HTML表看起来与FireFox和Internet Explorer中的相同。

在www.abcpdfeditor.com上有一个ABCpdf的在线演示。您可以使用它来检查您的表将如何首先呈现,而不需要下载和安装软件。

为了渲染整个网页,你需要AddImageUrl或AddImageHtml函数。但如果你想做的只是添加HTML样式的文本,那么你可以尝试AddHtml函数,如下所示:

Doc theDoc = new Doc();
theDoc.FontSize = 72;
theDoc.AddHtml("<b>Some HTML styled text</b>");
theDoc.Save(Server.MapPath("docaddhtml.pdf"));
theDoc.Clear();

ABCpdf是一个商业软件,但是标准版通常可以在特别优惠下免费获得。

我之前也在找这个。我遇到了HTMLDOC http://www.easysw.com/htmldoc/,这是一个免费的开源命令行应用程序,它以HTML文件为参数,并从中输出PDF。这在我的副业项目中非常有效,但这完全取决于你真正需要什么。

制作它的公司出售编译后的二进制文件,但您可以免费从源代码下载和编译并使用它。我设法编译了一个最近的版本(1.9版本),我打算在几天内发布它的二进制安装程序,所以如果你感兴趣,我可以在发布它时立即提供它的链接。

编辑(2/25/2014):看起来文档和网站转移到http://www.msweet.org/projects.php?Z1

不是直接将HTML解析为PDF,而是可以创建HTML页面的位图,然后将位图插入到PDF中,例如使用iTextSharp。

这是一个代码如何获得一个URL的位图。我在这里找到了它,如果我找到了源,我会链接它。

public System.Drawing.Bitmap HTMLToImage(String strHTML)
{
    System.Drawing.Bitmap myBitmap = null;

    System.Threading.Thread myThread = new System.Threading.Thread(delegate()
    {
        // create a hidden web browser, which will navigate to the page
        System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser();
        // we don't want scrollbars on our image
        myWebBrowser.ScrollBarsEnabled = false;
        // don't let any errors shine through
        myWebBrowser.ScriptErrorsSuppressed = true;
        // let's load up that page!    
        myWebBrowser.Navigate("about:blank");

        // wait until the page is fully loaded
        while (myWebBrowser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();

        myWebBrowser.Document.Body.InnerHtml = strHTML;

        // set the size of our web browser to be the same size as the page
        int intScrollPadding = 20;
        int intDocumentWidth = myWebBrowser.Document.Body.ScrollRectangle.Width + intScrollPadding;
        int intDocumentHeight = myWebBrowser.Document.Body.ScrollRectangle.Height + intScrollPadding;
        myWebBrowser.Width = intDocumentWidth;
        myWebBrowser.Height = intDocumentHeight;
        // a bitmap that we will draw to
        myBitmap = new System.Drawing.Bitmap(intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding);
        // draw the web browser to the bitmap
        myWebBrowser.DrawToBitmap(myBitmap, new System.Drawing.Rectangle(0, 0, intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding));
    });
    myThread.SetApartmentState(System.Threading.ApartmentState.STA);
    myThread.Start();
    myThread.Join();

    return myBitmap;
}

对于所有在。net 5及以上版本中寻找工作解决方案的人,请访问这里。

以下是我的工作解决方案。

使用wkhtmltopdf:

从这里下载并安装最新版本的wkhtmltopdf。 使用下面的代码。

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);
    }
}

并调用上述方法HtmlToPdf("test", new string[] {"https://www.google.com"}, new string[] {"-s A5"}); 如果你需要将HTML字符串转换为PDF,调整上述方法,并将参数替换为进程StartInfo为$@"/C echo | set /p=""{htmlText}"" | ""{pdfHtmlToPdfExePath}""{((选项== null) ?"":字符串。加入(“”,选项))}-”“C: \用户桌面\ xxxx \ \ {outputFilename}”“”;

这种方法的缺点:

wkhtmltopdf的最新版本不支持最新的HTML5和CSS3。因此,如果您尝试将任何html导出为CSS GRID,那么输出将不会像预期的那样。 您需要处理并发性问题。

使用铬无头:

从这里下载并安装最新的chrome浏览器。 使用下面的代码。

var p = new System.Diagnostics.Process()
{
    StartInfo =
    {
        FileName = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
        Arguments = @"/C --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf-no-header --print-to-pdf=""C:/Users/Abdul Rahman/Desktop/test.pdf"" ""C:/Users/Abdul Rahman/Desktop/grid.html""",
    }
};

p.Start();

// ...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();

这将把html文件转换为pdf文件。 如果你需要将一些url转换为pdf,那么使用下面的参数来处理StartInfo

@"/C——headless——disable-gpu——run-all- composter - stagebefore -draw——print-to-pdf-no-header——print-to-pdf=""C:/Users/Abdul Rahman/Desktop/test.pdf"" ""https://www.google.com"",

这种方法的缺点:

这与最新的HTML5和CSS3特性一样。输出将与您在浏览器中查看的相同,但当通过IIS运行时,您需要在LocalSystem Identity下运行应用程序的AppliactionPool,或者您需要提供对IISUSRS的读写访问。

使用Selenium WebDriver:

安装Nuget包硒。WebDriver和Selenium.WebDriver.ChromeDriver。 使用下面的代码。

public async Task<byte[]> ConvertHtmlToPdf(string html)
{
    var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "ApplicationName");
    Directory.CreateDirectory(directory);
    var filePath = Path.Combine(directory, $"{Guid.NewGuid()}.html");
    await File.WriteAllTextAsync(filePath, html);

    var driverOptions = new ChromeOptions();
    // In headless mode, PDF writing is enabled by default (tested with driver major version 85)
    driverOptions.AddArgument("headless");
    using var driver = new ChromeDriver(driverOptions);
    driver.Navigate().GoToUrl(filePath);

    // Output a PDF of the first page in A4 size at 90% scale
    var printOptions = new Dictionary<string, object>
    {
        { "paperWidth", 210 / 25.4 },
        { "paperHeight", 297 / 25.4 },
        { "scale", 0.9 },
        { "pageRanges", "1" }
    };
    var printOutput = driver.ExecuteChromeCommandWithResult("Page.printToPDF", printOptions) as Dictionary<string, object>;
    var pdf = Convert.FromBase64String(printOutput["data"] as string);

    File.Delete(filePath);

    return pdf;
}

该方法的优点:

这只需要一个Nuget安装和工作与最新的HTML5和CSS3功能预期。输出将与您在浏览器中查看的相同。

这种方法的缺点:

这种方法需要在应用程序运行的服务器上安装最新的chrome浏览器。 如果服务器中的chrome浏览器版本更新,则需要更新Selenium.WebDriver.ChromeDriver Nuget包。否则,由于版本不匹配,将抛出运行时错误。

使用这种方法,请确保在.csproj文件中添加<PublishChromeDriver>true</PublishChromeDriver>,如下所示:

<PropertyGroup>
  <TargetFramework>net5.0</TargetFramework>
  <LangVersion>latest</LangVersion>
  <Nullable>enable</Nullable>
  <PublishChromeDriver>true</PublishChromeDriver>
</PropertyGroup>

这将在发布项目时发布chrome驱动程序。

这是我的工作项目repo - HtmlToPdf的链接

使用JavaScript中的window.print()从浏览器生成PDF

如果用户从浏览器使用你的应用程序,那么你可以依靠JavaScript和使用window.print()和必要的打印媒体css从浏览器生成PDF。例如,生成发票从浏览器在一个库存应用程序。

该方法的优点:

不依赖于任何工具。 在浏览器中直接从HTML, CSS和JS生成PDF。 快 支持所有最新的CSS属性。

这种方法的缺点:

在像Blazor这样的SPA中,我们需要使用iframe来打印页面的部分。

在几乎花了2天的时间与可用的选项之后,我得到了上面的答案,最终实现了基于Selenium的解决方案,它正在工作。希望这能帮到你,节省你的时间。