【问题标题】:download html file as pdf using abcpdf使用 abcpdf 将 html 文件下载为 pdf
【发布时间】:2011-02-14 02:28:40
【问题描述】:

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

【问题讨论】:

    标签: asp.net html pdf download abcpdf


    【解决方案1】:

    这个方法在我们的项目中效果很好

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

    // 用法

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

    【讨论】:

      【解决方案2】:

      这就是您使用 ABCPdf 实现该目标的方式。

      http://www.websupergoo.com/helppdf7net/source/4-examples/13-pagedhtml.htm

      Doc theDoc = new Doc();
      theDoc.Rect.Inset(72, 144);
      
      theDoc.Page = theDoc.AddPage();
      int theID;
      theID = theDoc.AddImageUrl("http://www.yahoo.com/");
      
      while (true) {
        theDoc.FrameRect(); // add a black border
        if (!theDoc.Chainable(theID))
          break;
        theDoc.Page = theDoc.AddPage();
        theID = theDoc.AddImageToChain(theID);
      }
      
      for (int i = 1; i <= theDoc.PageCount; i++) {
        theDoc.PageNumber = i;
        theDoc.Flatten();
      }
      
      theDoc.Save(Server.MapPath("pagedhtml.pdf"));
      theDoc.Clear();
      

      【讨论】:

        【解决方案3】:

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

        <% @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();
        %>
        

        将 content-disposition 从“inline”更改为“attachment”会改变行为。

        产品文档中有一些关于 Doc.GetData() 函数的详细信息,您需要了解这些信息,并且您可能还会发现“Paged HTML Example”很有帮助。

        【讨论】:

          猜你喜欢
          • 2012-05-09
          • 1970-01-01
          • 2012-03-22
          • 2012-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-02
          • 1970-01-01
          相关资源
          最近更新 更多