read pdf from url to memory omitting saving file to local file

Viewed 1238

I have a problem with omitting saving file to a local file. My problem is how to pass directly the url content to the pdfplumber module. My code is as follows

from pathlib import Path
import urllib3
import pdfplumber

def read_pdf(url: str) -> str:
    http = urllib3.PoolManager()
    temp = Path('temp.pdf')
    temp.write_bytes(http.request("GET", url).data)
    with pdfplumber.open(temp) as pdf:
        text = pdf.pages[0].extract_text()
    temp.unlink()
    return text

text = read_pdf('https://sample.com/test.pdf')

It works but I would like to avoid saving file to local but do it in the RAM only. I have tried io.BytesIO but it doesn't work

2 Answers

Unlike previous suggestion, you should use pdfplumber.open instead of pdfplumber.load! The link for the repo of pdfplumber.

Warning: pdfplumber.load is deprecated.Please use pdfplumber.open (with same arguments) instead.

Hence, the below code extracts entire pages rather than the first page in addition to using pdfplumber.open method.

import urllib3
import pdfplumber
import io


def extract_pdf_by_url(url):
    http = urllib3.PoolManager()
    temp = io.BytesIO()
    temp.write(http.request("GET", url).data)
    all_text = ''
    with pdfplumber.open(temp) as pdf:
        for pdf_page in pdf.pages:
            single_page_text = pdf_page.extract_text()
            all_text = all_text + '\n' + single_page_text
    return all_text

url  = 'https://raw.githubusercontent.com/jsvine/pdfplumber/stable/examples/pdfs/background-checks.pdf'
text = extract_pdf_by_url(url)
print(text)

Here is an example, which uses io.BytesIO() instead of creating any files on the hard drive.
Note that pdfplumber.load(...) is used instead of pdfplumber.open(...).

import urllib3
import pdfplumber
import io


def read_pdf(url: str) -> str:
    http = urllib3.PoolManager()
    temp = io.BytesIO()
    temp.write(http.request("GET", url).data)
    with pdfplumber.load(temp) as pdf:
        text = pdf.pages[0].extract_text()
    return text

text = read_pdf('https://sample.com/test.pdf')
Related