Asp.net 使用abcpdf将html文件下载为pdf

Asp.net 使用abcpdf将html文件下载为pdf,asp.net,html,pdf,download,abcpdf,Asp.net,Html,Pdf,Download,Abcpdf,如何使用ASP.Net中的abcpdf将HTML文件下载为PDF,C#?以下C#中的ASP.Net示例显示了如何从网页创建PDF并将其流式传输到web浏览器 <% @Page Language="C#" %> <% @Import Namespace="WebSupergoo.ABCpdf7" %> <% Doc theDoc = new Doc(); theDoc.AddImageUrl("http://www.example.com"); byte[] theD

如何使用ASP.Net中的abcpdf将HTML文件下载为PDF,C#?

以下C#中的ASP.Net示例显示了如何从网页创建PDF并将其流式传输到web浏览器

<% @Page Language="C#" %>
<% @Import Namespace="WebSupergoo.ABCpdf7" %>
<%
Doc theDoc = new Doc();
theDoc.AddImageUrl("http://www.example.com");
byte[] theData = theDoc.GetData();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.AddHeader("content-length", theData.Length.ToString());
Response.BinaryWrite(theData);
Response.End();
%>

将内容处置从“内联”更改为“附件”将更改行为


产品文档中有一些关于()函数的详细信息,您需要了解,您可能会发现“”也很有用。

这就是如何使用ABCPdf来实现该目标

Doc theDoc=new Doc();
DOC.Rect.插图(72144);
theDoc.Page=theDoc.AddPage();
int theID;
theID=doc.AddImageUrl(“http://www.yahoo.com/");
while(true){
theDoc.FrameRect();//添加黑色边框
如果(!theDoc.Chaineable(theID))
打破
theDoc.Page=theDoc.AddPage();
theID=DOC.AddImageToChain(theID);
}

对于(inti=1;i,此方法在我们的项目中效果良好

    /// <summary>
    /// Converts Html to pdf
    /// </summary>
    /// <param name="htmlOrUrl">Html markup of html page URL</param>
    /// <param name="isUrl">previous parameter is URL</param>
    /// <param name="highQuality">use high quality converter engine</param>
    /// <param name="indent">indent from all sides of the page</param>
    /// <returns>Memory stream with PDF-file</returns>
    public static MemoryStream HtmlToPDF(this String htmlOrUrl, Boolean isUrl, Boolean highQuality = false, Int32 indent = 20)
    {
        using (var doc = new Doc())
        {
            doc.Color.String = "0, 0, 0";
            doc.HtmlOptions.UseScript = true;
            doc.HtmlOptions.AddLinks = true;
            if (highQuality)
            {
                doc.HtmlOptions.Engine = EngineType.Gecko;
            }

            // 1. CONTENT BLOCK
            doc.Rect.Left = 0 + indent;
            doc.Rect.Top = 792 - indent;
            doc.Rect.Right = 612 - indent;
            doc.Rect.Bottom = 0 + indent;

            doc.AppendChainable(htmlOrUrl, isUrl);

            var ms = new MemoryStream();
            doc.Save(ms);
            if (ms.CanSeek)
            {
                ms.Seek(0, SeekOrigin.Begin);
            }
            return ms;
        }
    }

    /// <summary>
    /// Appends document with multipage content 
    /// </summary>
    private static void AppendChainable(this Doc doc, String htmlOrUrl, Boolean isUrl = false)
    {
        Int32 blockId = isUrl 
            ? doc.AddImageUrl(htmlOrUrl) 
            : doc.AddImageHtml(String.Format(HtmlWrapper, htmlOrUrl));

        while (doc.Chainable(blockId))
        {
            //doc.FrameRect(); // add a black border
            doc.Page = doc.AddPage();
            blockId = doc.AddImageToChain(blockId);
        }
    }
//并且流到文件是

/// <summary>
/// Saves stream instance to file
/// </summary>
public static void StreamToFile(this MemoryStream input, String outputFileName)
{
    var dirName = Path.GetDirectoryName(outputFileName);
    var fileName = Path.GetFileName(outputFileName);
    if (String.IsNullOrEmpty(dirName) || String.IsNullOrWhiteSpace(fileName))
    {
        throw new IOException("outputFileName");
    }

    using (FileStream outStream = File.Create(outputFileName))
    {
        input.WriteTo(outStream);
        outStream.Flush();
        outStream.Close();
    }
}
//
///将流实例保存到文件
/// 
公共静态void StreamToFile(此MemoryStream输入,字符串输出文件名)
{
var dirName=Path.GetDirectoryName(outputFileName);
var fileName=Path.GetFileName(outputFileName);
if(String.IsNullOrEmpty(dirName)| | String.IsNullOrWhiteSpace(fileName))
{
抛出新IOException(“outputFileName”);
}
使用(FileStream outStream=File.Create(outputFileName))
{
输入。写入(流出);
冲水;
exptream.Close();
}
}
var testMs1 = ABCPdfConverter.ABCConverter.HtmlToPDF("https://developers.google.com
   /chart/interactive/docs/examples", true, false, 20);
testMs1.StreamToFile(@"D:/3.pdf");
/// <summary>
/// Saves stream instance to file
/// </summary>
public static void StreamToFile(this MemoryStream input, String outputFileName)
{
    var dirName = Path.GetDirectoryName(outputFileName);
    var fileName = Path.GetFileName(outputFileName);
    if (String.IsNullOrEmpty(dirName) || String.IsNullOrWhiteSpace(fileName))
    {
        throw new IOException("outputFileName");
    }

    using (FileStream outStream = File.Create(outputFileName))
    {
        input.WriteTo(outStream);
        outStream.Flush();
        outStream.Close();
    }
}