使用 pyPdf 打开 pdf url
问题描述:
如何从 url 而不是从磁盘打开 pdf
How would I open a pdf from url instead of from the disk
类似的东西
input1 = PdfFileReader(file("http://example.com/a.pdf", "rb"))
我想从网上打开几个文件并下载所有文件的合并.
I want to open several files from web and download a merge of all the files.
答
我认为 urllib2 会给你什么你想要.
I think urllib2 will get you what you want.
from urllib2 import Request, urlopen
from pyPdf import PdfFileWriter, PdfFileReader
from StringIO import StringIO
url = "http://www.silicontao.com/ProgrammingGuide/other/beejnet.pdf"
writer = PdfFileWriter()
remoteFile = urlopen(Request(url)).read()
memoryFile = StringIO(remoteFile)
pdfFile = PdfFileReader(memoryFile)
for pageNum in xrange(pdfFile.getNumPages()):
currentPage = pdfFile.getPage(pageNum)
#currentPage.mergePage(watermark.getPage(0))
writer.addPage(currentPage)
outputStream = open("output.pdf","wb")
writer.write(outputStream)
outputStream.close()