54

The following code allows me to view a png image in an iPython notebook. Is there a way to view pdf image? I don't need to use IPython.display necessarily. I am looking for a way to print a pdf image in a file to the iPython notebook output cell.

## This is for an `png` image
from IPython.display import Image

fig = Image(filename=('./temp/my_plot.png'))
fig

Thank you.

5 Answers 5

88

The problem you (and others) face is that PDFs cannot be displayed directly in the browser. The only possible way to get something similar is to use an image-converter to create a PNG or JPG out of the PDF and display this one.
This could be done via imagemagick and a custom display function.

Update 1

A simple solution is to use wand (http://docs.wand-py.org) a python-imagemagick binding. I tried with Ubuntu 13.04:

wand session in ipython

In text form:

from wand.image import Image as WImage
img = WImage(filename='hat.pdf')
img

For a multi-page pdf, you can get e.g. the second page via:

img = WImage(filename='hat.pdf[1]')

Update 2

As recent browsers support to display pdfs with their embedded pdf viewer a possible alternative solution based on an iframe can be implemented as

class PDF(object):
  def __init__(self, pdf, size=(200,200)):
    self.pdf = pdf
    self.size = size

  def _repr_html_(self):
    return '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.pdf, self.size)

  def _repr_latex_(self):
    return r'\includegraphics[width=1.0\textwidth]{{{0}}}'.format(self.pdf)

This class implements html and latex representations, hence the pdf will also survive a nbconversion to latex. It can be used like

PDF('hat.pdf',size=(300,250))

With Firefox 33 this results in enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Very cool! Can wand pick a particular page out of a pdf? If so, an example of that would be great.
Yeah, you can use hat.pdf[1] to access the second page of hat.pdf. It's exactly the same as with standard IM see e.g. stackoverflow.com/q/4809314/2870069
How do you display multiple pages? IE hat.pdf[1-5]
@AdamHughes what do you want to see in such a case? Wand converts the pdf to an Image, hence do you want a large image of all pages
@AdamHughes I 've updated my answer to show a second possibility based on an iframe with embedded pdf viewer. Maybe this approach is more appropriate for you. If not, I'd suggest to use pdfnup to create a onepage pdf with all your pages arranged as desired and subsequently create the wand image.
|
82

To show pdf-s inside ipython/jupyter notebooks you can use IFrame

from IPython.display import IFrame
IFrame("./samples/simple3.pdf", width=600, height=300)

Here is the screenshot

pdf preview in ipython/jupyter notebook

4 Comments

Smart solution, and useful for so much more
Hi can we put the link of files stored on a repository in Github and get it embedded like this? I tried but it doesn't seem to work
Extremely important note: the target PDF file must be in a subdirectory of the directory jupyter notebook was run on.
Even if the pdf is in a subfolder, it doesn't work in Safari as of now. In firefox and chrome it works.
5

In addition to Jakob's excellent answer recommending the Wand bindings for ImageMagick:

If your PDF contains vector graphics, use the resolution keyword to control the size of the rendered image. ImageMagick's default value is 72 dpi. Higher values yield more pixels.

from wand.image import Image as WImage
img = WImage(filename='hat.pdf', resolution=100) # bigger
img

Comments

4

Assuming a multi-image pdf called Rplots.pdf

The following works in the jupyter notebook cell. For installation I used

pip install Wand

This code pastes into a cell

from wand.image import Image  

imageFromPdf = Image(filename='Rplots.pdf')  
pages = len(imageFromPdf.sequence)  

image = Image(  
  width=imageFromPdf.width,  
  height=imageFromPdf.height * pages  
)  
for i in range(pages):  
  image.composite(  
  imageFromPdf.sequence[i],  
  top=imageFromPdf.height * i,  
  left=0  
)  
image.format="png"  
image 

Comments

1

I think there is a better way to do this, since

  • with wand you need to install the MagickWand library
  • with IFrame you are not flexible with the directories

I would do it like this:

    from PIL import Image
    import matplotlib.pyplot as plt
    import fitz

    your_path = # YOUR PATH
    doc = fitz.open(your_path)
    MAX_PAGES = 1

    zoom = 2 # to increase the resolution
    mat = fitz.Matrix(zoom, zoom)

    for i, page in enumerate(doc):
        pix = page.get_pixmap(matrix=mat)
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

        # display images
        plt.figure(figsize=(7,7), facecolor="w")
        plt.xticks(color="white")
        plt.yticks(color="white")
        plt.tick_params(bottom = False)
        plt.tick_params(left = False)

        plt.imshow(img)

        if i > MAX_PAGES - 1:
            break

2 Comments

This is a great answer if you have to run everything in a notebook and can't install packages via the CLI and your pdfs are in S3. I am running on sagemaker and just had to add some small changes for opening the file: s3 = boto3.client('s3'); pdf_file = s3.get_object(Bucket=bucket_name, Key=pdf_file_name); file_content = pdf_file['Body'].read(); with fitz.open(stream=file_content, filetype="pdf") as doc: for i, page in enumerate(doc):
This is great is because with the old Wand approach I just got PolicyError: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/426, while this approach worked! However, could you please change not to use fitz. Everywhere you have fitz should be pymupdf, such as import pymupdf, see Problems after installation and note the current note at the bottom of repo linked from pypi fitz at this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.