How to display a pdf that has been downloaded in python
Asked Answered
C

6

9

I have grabbed a pdf from the web using for example

import requests
pdf = requests.get("http://www.scala-lang.org/docu/files/ScalaByExample.pdf")

I would like to modify this code to display it

from gi.repository import Poppler, Gtk

def draw(widget, surface):
    page.render(surface)

document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
page = document.get_page(0)

window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)

window.show_all()
Gtk.main()

How do I modify the document = line to use the variable pdf that contains the pdf?

(I don't mind using popplerqt4 or anything else if that makes it easier.)

Catamnesis answered 10/2, 2014 at 17:46 Comment(2)
You should be using Poppler.Document.new_from_data, however there is a conversion problem between str and char * due to the way str is expected to carry Unicode data, but char * expects raw binary data. Up to now, I couldn't make it work.Corron
@Corron Good Idea ! I used len(repr(content)) for length field and str(content) for the data field. It worked for me.Warfold
B
5

It all depends on the OS your using. These might usually help:

import os
os.system('my_pdf.pdf')

or

os.startfile('path_to_pdf.pdf')

or

import webbrowser
webbrowser.open(r'file:///my_pdf.pdf')
Biondo answered 16/10, 2016 at 6:11 Comment(0)
C
1

How about using a temporary file?

import tempfile
import urllib
import urlparse

import requests

from gi.repository import Poppler, Gtk

pdf = requests.get("http://www.scala-lang.org/docu/files/ScalaByExample.pdf")

with tempfile.NamedTemporaryFile() as pdf_contents:
    pdf_contents.file.write(pdf)
    file_url = urlparse.urljoin(
        'file:', urllib.pathname2url(pdf_contents.name))
    document = Poppler.Document.new_from_file(file_url, None)
Carlyncarlynn answered 13/2, 2014 at 14:0 Comment(4)
This is my current workaround. It would be great if it could be avoided however.Catamnesis
Are you using python-poppler-qt4, pypoppler, or which library is the one that defines Document.Poppler ?Carlyncarlynn
My import line is from gi.repository import Poppler, Gtk which defines Poppler.Document . I needed to install libpoppler-dev to get it to work I think. I am happy to move to python-poppler-qt if that is a good idea however.Catamnesis
And which library is, in turn, the one that allows you to import gi.repository ? :) BTW, I am not suggesting you move to another library, I do not have very much experience with the others I mentioned ...Carlyncarlynn
W
1

Try this and tell me if it works:

document = Poppler.Document.new_from_data(str(pdf.content),len(repr(pdf.content)),None)
Warfold answered 19/2, 2014 at 18:34 Comment(2)
I still get PDF document is damaged with this solution with python3.3, and a segmentation fault on python2.7. But maybe it will work for OP...Corron
I tried it in ipython notebook. It did. but since @Corron says it did not work for him. You should try it yourself and tell me if it does work for you.Warfold
M
1

If you want to open pdf using acrobat reader then below code should work

import subprocess
process = subprocess.Popen(['<here path to acrobat.exe>', '/A', 'page=1', '<here path to pdf>'], shell=False, stdout=subprocess.PIPE)
process.wait()
Marauding answered 19/2, 2014 at 19:10 Comment(0)
A
1

August 2015 : On a fresh intallation in Windows 7, the problem is still the same :

Poppler.Document.new_from_data(data, len(data), None)

returns : Type error: must be strings not bytes.

Poppler.Document.new_from_data(str(data), len(data), None)

returns : PDF document is damaged (4).

I have been unable to use this function.

I tried to use a NamedTemporayFile instead of a file on disk, but for un unknown reason, it returns an unknown error.
So I am using a temporary file. Not the prettiest way, but it works.

Here is the test code for Python 3.4, if anyone has an idea :

from gi.repository import Poppler
import tempfile, urllib
from urllib.parse import urlparse
from urllib.request import urljoin

testfile = "d:/Mes Documents/en cours/PdfBooklet3/tempfiles/preview.pdf"
document = Poppler.Document.new_from_file("file:///" + testfile, None)          # Works fine
page = document.get_page(0)
print(page)         # OK

f1 = open(testfile, "rb")
data1 = f1.read()
f1.close()

data2 = "".join(map(chr, data1))  # converts bytes to string
print(len(data1))
document = Poppler.Document.new_from_data(data2, len(data2),  None)
page = document.get_page(0)                                                     # returns None
print(page)

pdftempfile = tempfile.NamedTemporaryFile()
pdftempfile.write(data1)

file_url = urllib.parse.urljoin('file:', urllib.request.pathname2url(pdftempfile.name))
print( file_url)
pdftempfile.seek(0)
document = Poppler.Document.new_from_file(file_url, None)                       # unknown error
Appendage answered 15/8, 2015 at 14:53 Comment(1)
Don't cast the bytes to a string, rather decode them.Plebe
O
0

Since there is a library named pyPdf, you should be able to load PDF file using that. If you have any further questions, send me messege.

Overplus answered 1/3, 2014 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.