zoukankan      html  css  js  c++  java
  • 将PDF文件中按页截取,并以流的形势返回给客户端

    #需要用到pyPdf库
    from pyPdf import PdfFileWriter,PdfFileReader
    from django.http import HttpResponse
    
    try:
        from cStringIO import StringIO
    except:
        from io import BytesIo as StringIo
    
    
    def split_pdf(request):
        pdf_file = u'C:/Users/admin/Desktop/xxxx.pdf'
        page = 1 #返回的页码
        try:
            currentPage = int(page)
        except:pass
    
        pdfFile = file(pdf_file, 'rb')   #打开pdf
    
        pdf_input = PdfFileReader(pdfFile) #创建对象
        #获取当前PDF的总页数
        pageCount = pdf_input.getNumPages()
    
        if currentPage:
            if currentPage < 0 or currentPage >pageCount:
                currentPage = 1
            #获取指定PDF指定页面,以流的形式返回给客户端
            out = StringIO()  # 获取管道
            p = PdfFileWriter()  #创建写入对象
            p.addPage(pdf_input.getPage(currentPage))  #将当前页对象添加到创建的pdf最后面
            p.write(out) #将pdf写入流 
    
            out.seek(0) #移动文件指针到第0个位置
            response = HttpResponse(content_type='application/pdf')  
            response.write(out.read())
            return response
  • 相关阅读:
    文件夹打开对话框
    文件打开对话框
    HOOK函数(二)——全局HOOK
    HOOK函数(一)——进程内HOOK
    抓包
    List 访问
    坑爹的EL 表达式。
    tomcat 虚拟目录的安全问题
    框架
    程序员相关词汇
  • 原文地址:https://www.cnblogs.com/os-python/p/7843075.html
Copyright © 2011-2022 走看看