zoukankan      html  css  js  c++  java
  • 网页转化为PDF,并用流输出.

    客户有个需求,就是需要保存当前网页为PDF.

    1.直接使用非常成熟的类.ABCpdf.

    下载下来以后,从安装包里面,找到几个dll文件即可.:ABCpdf.dll,ABCpdfCE7.dll,

    2.新建文件.引入命名空间

       using WebSupergoo.ABCpdf7;
       using WebSupergoo.ABCpdf7.Objects;
       using WebSupergoo.ABCpdf7.Atoms;

    3.直接上代码,该代码会直接把文档以流的方式输出.如需生成文件.直接修改theDoc.Save()中参数即可.

       

                Doc theDoc = new Doc();  //创建一个Doc对象
    XSettings.License = "key";

    theDoc.Font = theDoc.AddFont("宋体", "ChineseS");
    theDoc.Rect.Inset(24, 48);
    //Rect默认是文档整个页面大小, 这里的Inset表示将Rect左右留出24的空白,上下留出48的空白

            theDoc.MediaBox.String = "0 0 810 480";  //设置添加新页面时,页面的大小,即文档的大小.
            theDoc.Rect.String = "10 0 800 485";     //当前输出区间,即显示的大小.

               int theID = theDoc.AddImageUrl(url, true, 1000, false);

           
           //PDF分页
    while (true)
    {
    if (!theDoc.Chainable(theID))
    break;
    theDoc.Page = theDoc.AddPage();
    theID = theDoc.AddImageToChain(theID);
    }
    string sFile = user_Id.ToString();
    Response.Clear();
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + sFile + ".pdf");
    Response.ContentType = "application/octet-stream";
    //直接输出流
    theDoc.Save(Response.OutputStream);
    Response.Flush();
    theDoc.Clear();


    4.需要注意的是,出现 HTML render is blank. 的错误...经我发现.这是由于AddImageUrl(url)..这个方法中..可能url的这个页面.对session的判定.使插件不能访问这个页面,需要删除那个页面的session判断即可.

  • 相关阅读:
    css技巧和经验列表
    CSS3嵌入字体@font-face调用字体
    新闻列表单行滚动(多行显示)简洁向上滚动js效果
    打破构图的平衡!增强设计感染力
    何以双十(补昨天)
    MySQL5.7 基于二进制包的安装
    Nginx Log日志统计分析常用命令
    MySQL错误代码大全
    MVC4中的Display Mode简介
    ReadOnly关键字修饰的变量可以修改,只是不能重新分配
  • 原文地址:https://www.cnblogs.com/eastday/p/2371849.html
Copyright © 2011-2022 走看看