65

Is this possible? Everything I'm google leads me to converting markdown to PDF. But I want to display a PDF image in a markdown file. This is my example:

![hustlin_erd](erd.pdf)

If i use a regular image, it works just fine. Also I'm aware of converting PDF to image.

Andrew Kim
  • 3,145
  • 4
  • 22
  • 42

2 Answers2

80

Markdown itself doesn't have a mechanism for embedding a PDF. However, Markdown accepts raw HTML in its input and passes it through unaltered. So the question you might want to ask is: How would you embed a PDF in HTML? In other words, what HTML would one use to have a browser display a PDF embedded in an HTML page? You would just include that HTML in your Markdown document.

You can find lots of suggestions in answers to the question: Recommended way to embed PDF in HTML?. For example, this answer provides a nice solution with a fallback for older browsers (all credit goes to Suneel Omrey):

<object data="http://yoursite.com/the.pdf" type="application/pdf" width="700px" height="700px">
    <embed src="http://yoursite.com/the.pdf">
        <p>This browser does not support PDFs. Please download the PDF to view it: <a href="http://yoursite.com/the.pdf">Download PDF</a>.</p>
    </embed>
</object>
amc
  • 813
  • 1
  • 15
  • 28
Waylan
  • 37,164
  • 12
  • 83
  • 109
  • 7
    @FelipeS.S.Schneider I expect GitHub's sanitation filter will remove the`object` and `embed` tags ([step 2 in this list](https://github.com/github/markup#github-markup)). So, no it probably won't work on gitHub.com. – Waylan May 31 '19 at 00:17
5

I have found a better way by using xfun R package.

xfun::embed_file("homework.pdf")

This will embed your pdf file in the output of the rmarkdown. More information can be seen via link.

I hope you will enjoy this.

gbganalyst
  • 364
  • 4
  • 8
  • 2
    This is useful, but it doesn't actually display the file. It displays the link to the file that the reader has to click on (indeed explained in the link)., and the file will open in a separate window following clicking on the link. – Wera Sep 23 '22 at 11:57