网页转化为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判断即可.

 

转载于:https://www.cnblogs.com/eastday/archive/2012/02/28/2371849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于pdf文件是一种矢量图形格式,需要先将其转换为位图格式,才能被opencv读取和处理。可以使用Python的pdf2image库将pdf文件转换为图片格式,然后使用opencv的imread函数读取图片文件。 以下是一段示例代码,可以将pdf文件转换为png格式的图片,并使用opencv的imdecode函数将图片数据读入内存,然后使用requests库将图片数据发送到指定的URL地址: ```python import requests from pdf2image import convert_from_path import cv2 import numpy as np # 定义要转换的pdf文件路径和输出图片的格式和路径 pdf_path = 'test.pdf' img_format = 'png' img_path = 'test.png' # 将pdf文件转换为png格式的图片 images = convert_from_path(pdf_path, fmt=img_format) image = images[0] # 取第一页作为示例 # 将图片转换为numpy数组格式 img_array = np.array(image) # 将图片数组编码为JPEG格式 retval, buffer = cv2.imencode('.jpg', img_array) # 将编码后的数据解码为numpy数组 img_data = np.asarray(buffer) # 使用requests库发送图片数据到指定URL地址 url = 'http://example.com/upload' headers = {'Content-Type': 'image/jpeg'} response = requests.post(url, headers=headers, data=img_data.tostring()) # 输出响应结果 print(response.text) ``` 注意,由于图片数据是存储在内存中的,因此需要将其转换为字节串格式,才能通过requests库发送出去。此外,还需要指定正确的Content-Type头部信息,以指示接收方该如何解析数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值