Repository: ipython/ipython

Issue:
Embedding PDFs
I would like to add support for embedding PDF files into ipython notebooks. I understand that browsers differ in their ability to display PDFs, but I think there are several ways to get around this issue and the pros of including this functionality outweigh the cons:
- the current standard vector format is SVG, which is apparently preferred by the open source community, but is not generally accepted for use in the scientific community -- I typically have to embed a png into ipython while I'm working and export a PDF separately for presentation and publication purposes 
- SVG is poorly supported on the Mac (and Adobe Illustrator can't handle even basic SVGs properly), whereas PDF is a native format
- the notebook to latex export requires converting SVG to PDF

A quick proof-of-principle notebook shows that using `IPython.display.HTML` to wrap an HTML `<object>` tag with the PDF data embedded works fine in at least Safari, Chrome and (with a PDF viewer plugin) Firefox.

Adding this functionality to IPython itself would be great. I'm happy to work on this if there's interest and someone can guide me with some of the details on the actual publishing and nbconversion that would make this great.

Just to be clear, I'm not using pylab/matplotlib to create the PDFs -- I realize that there's some work done on linking PDFs created using that system (but I couldn't find a general purpose API for embedding PDFs).



Discussion:
As you point out, you can already do this easily:

``` python
from IPython.display import HTML
HTML('<iframe src=http://fperez.org/papers/ipython07_pe-gr_cise.pdf width=700 height=350></iframe>')
```

And if you have to do it a lot, a function:

``` python
def PDF(url):
    return HTML('<iframe src=%s width=700 height=350></iframe>' % url)
```

The challenge (I suspect) with making this a core feature is that the subtle details of PDF support vary by browser & plugin.

yeah, so the HTML part is easy enough -- any tips on getting it to embed the PDF in the latex/pdf output?

You could simply create some sort of PDF class (similar to the PNG, Latex, HTML,.. ones) and implement the required `_repr_` methods. A simple solution could look like:

``` python
class PDF(object):
  def __init__(self,url):
    self.url = url

  def _repr_html_(self):
    return '<iframe src=%s width=300 height=300></iframe>' % self.url

  def _repr_latex_(self):
    return r'\begin{center} \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{%s}\end{center}' % self.url
```

One tricky part in your solution is to get a reasonable size for the iframe to render the pdf. I'm not a big fan of big gray boxes with little content in my notebook, that's why I suggested an Imagemagick based approach using [Wand](http://docs.wand-py.org/en/0.3.7/), see [this Stackoverflow question](http://stackoverflow.com/a/19470377/2870069).

The notebook frontend and nbconvert already support pdf. If you create an
object with a `_repr_pdf_` method that returns the raw PDF data it will
provide a link to view the PDF in the notebook UI and treat it as a PDF
figure in nbconvert. Probably not optimal for this usage case, but could be
used as a starting place for a better solution...

On Mon, Jun 23, 2014 at 2:37 PM, Jakob Gager notifications@github.com
wrote:

> You could simply create some sort of PDF class (similar to the PNG, Latex,
> HTML,.. ones) and implement the required _repr_ methods. A simple
> solution could look like:
> 
> class PDF(object):
>   def **init**(self,url):
>     self.url = url
> 
>   def _repr_html_(self):
>     return '<iframe src=%s width=300 height=300></iframe>' % self.url
> 
>   def _repr_latex_(self):
>     return r'\begin{center} \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{%s}\end{center}' % self.url
> 
> One tricky part in your solution is to get a reasonable size for the
> iframe to render the pdf. I'm not a big fan of big gray boxes with little
> content in my notebook, that's why I suggested an Imagemagick based
> approach using Wand http://docs.wand-py.org/en/0.3.7/, see this
> Stackoverflow question http://stackoverflow.com/a/19470377/2870069.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/ipython/ipython/issues/5791#issuecomment-46905637.

## 

Brian E. Granger
Cal Poly State University, San Luis Obispo
@ellisonbg on Twitter and GitHub
bgranger@calpoly.edu and ellisonbg@gmail.com


Resolving patch:
diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py
--- a/IPython/core/formatters.py
+++ b/IPython/core/formatters.py
@@ -837,6 +837,8 @@ class PDFFormatter(BaseFormatter):
 
     print_method = ObjectName('_repr_pdf_')
 
+    _return_type = (bytes, unicode_type)
+
 
 FormatterABC.register(BaseFormatter)
 FormatterABC.register(PlainTextFormatter)


Test patch: