简体   繁体   中英

python pdf2image from link “Unable to get page count”

I have a PDF link I want to convert to an Image so I ran this

import requests
import pdf2image
x = "https://www.criticallink.com/wp-content/uploads/ISO-9001-2015-Certificate.pdf"
pdf = requests.get(x,stream=True,timeout=30)
images = pdf2image.convert_from_bytes(pdf.raw.read())

but I get this error

PDFPageCountError: Unable to get page count.
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error (19): Illegal character '>'
Syntax Error (46): Illegal character ')'
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table

what should I do?

UPDATE:

pdf.raw.read()[:100]
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x9c\xfdy@Rk\xfc>\x8a\xbe\x80\x8a\x9aC\x03\x15\x9aSY\n\xb5SI\xcaY\xd1\xb6\x13N\x80\xed\xdc\x91\x99i)X\x9a8U\x98\x8a\xd9\xb4\xd9\x84\x9a\x94F\x0e\x14\xa0\xb5\xcb\xac\x1d\xa6V\xa6\rH\xb5\xb7\xa2hZ6\x99\x9aJdj\xe2\x909\xdc\xe5\xfe}\xcf\xf9\xdd{\xcf\xf9\xe3\x9e\xbb\xfa#'

So you're getting a GZIP-encoded response. Try the following.

import gzip
import requests
import pdf2image

url = "https://www.criticallink.com/wp-content/uploads/ISO-9001-2015-Certificate.pdf"
response = requests.get(url, stream=True, timeout=30)
pdf = gzip.open(response.raw)
images = pdf2image.convert_from_bytes(pdf.read())

Alternatively, you can use

import requests
import pdf2image

url = "https://www.criticallink.com/wp-content/uploads/ISO-9001-2015-Certificate.pdf"
response = requests.get(url, timeout=30)
images = pdf2image.convert_from_bytes(response.content)

and let requests do the decoding for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM