How to embed PDF in html

Published: less than 1 minute read, with 142 words. Post views:

I tried to embed my CV in my homepage, and found that there are mainly three options to include PDF in html:

  1. object / embed: <object data="document.pdf" width="1000" height="1000" type='application/pdf'></object> or <embed src="document.pdf" width="100%" height="100%" type="application/pdf" />
    • Pros: Easy to use.
    • Cons: Desktop and mobile Safari will stuck when using this method.
  2. iframe: <embed src="document.pdf" width="100%" height="100%" ></iframe>
    • Pros: Easy to use.
    • Cons: Mobile Safari will only show the first page, can not scroll.
  3. pdf.js: <iframe src="/web/viewer.html?file=document.pdf" width="100%" height="800"></iframe>
    • Pros: works fine in both mobile and desktop browsers.
    • Cons: Not natively supported in HTML, needed external libraries.
    • Notes:
      1. The legacy version works fine, but the modern version does not work well in my desktop Safari.
      2. Some configurations are useful, e.g., #pagemode=none.

References:

  1. 记录一些曾遇到的前端问题 - 书童小二的文章 - 知乎
  2. Safari on Big Sur freezes when embedding PDF file
  3. Make embedded PDF scrollable in iPad

Tags: ,

Categories:

Comments