# Oneboxing of PDFs and other attachments

**URL:** https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972
**Category:** Feature
**Created:** [2017 年1 月 18 日 16:20 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972 "2017-01-18T16:20:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### 作者： ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### 发布日期： [2017 年1 月 18 日 16:20 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/1 "2017-01-18T16:20:13Z")

</div>

Continuing the discussion from [Custom visualization for specific attachment types](https://meta.discourse.org/t/custom-visualization-for-specific-attachment-types/14941/16):

Putting this out there as a feature request. I’d love to see the ability to onebox PDFs and other attachments along the lines of google docs oneboxing. Or perhaps even simply a file attachment appearance like you’d get using the file upload, ideally also with the file type and size provided.

Right now putting a PDF URL on its own line presents as a raw URL. Very 1990s. As my millenial colleague told me recently, “who needs to know what http is in this day and age?”

---

<div class="post-metadata">

### 作者： ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### 发布日期： [2017 年1 月 18 日 16:40 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/2 "2017-01-18T16:40:55Z")

</div>

> [@tobiaseigen](#):
>
> As my millennial colleague told me recently, “who needs to know what http is in this day and age?”

Kids these days. I’m so sure.

---

<div class="post-metadata">

### 作者： ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### 发布日期： [2017 年1 月 18 日 23:32 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/3 "2017-01-18T23:32:13Z")

</div>

Sure oneboxing of PDF is a reasonable idea, @techapj can you add it to your list? At minimum try to get the **title** of the document and a text summary. I would not worry about thumbnail as that will be considerably harder, just use a generic (but pretty) PDF icon like we do for Google Docs.

---

<div class="post-metadata">

### 作者： ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### 发布日期： [2017 年2 月 6 日 19:30 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/4 "2017-02-06T19:30:33Z")

</div>

Okay, we now support PDF onebox using [PDF metadata](https://helpx.adobe.com/acrobat/using/pdf-properties-metadata.html).

The oneboxing works best when the metadata of PDF file is complete i.e. it contains “Title”, “Subject” & “Author”.

Demo:

1. PDF contains complete metadata:

[https://www.wired.com/images\_blogs/threatlevel/2009/12/redaction.pdf](https://www.wired.com/images_blogs/threatlevel/2009/12/redaction.pdf)

1. PDF only have “Title” & “Author” as metadata:

> **[Black\_Cat.pdf](https://www.ibiblio.org/ebooks/Poe/Black_Cat.pdf)**
>
> 80.34 KB

1. PDF with no metadata:

> **[Sample\_Syllabus\_for\_Wikipedia\_assignment.pdf](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sample_Syllabus_for_Wikipedia_assignment.pdf)**
>
> 1993.87 KB

---

<div class="post-metadata">

### 作者： ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### 发布日期： [2017 年2 月 7 日 16:00 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/5 "2017-02-07T16:00:21Z")

</div>

Thanks for this - super exciting to see PDF oneboxing. 🚀

However I’m having a bit of trouble with it - my PDFs do not appear to be oneboxed, even here on meta. Here’s an example:

[https://namati.org/wp-content/uploads/2017/01/4.Evidence\_Land-Rights\_-Myanmar-2017-Final.pdf](https://namati.org/wp-content/uploads/2017/01/4.Evidence_Land-Rights_-Myanmar-2017-Final.pdf)

---

<div class="post-metadata">

### 作者： ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### 发布日期： [2017 年2 月 8 日 07:34 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/6 "2017-02-08T07:34:32Z")

</div>

> [@techAPJ](#):
>
> PDF onebox using PDF metadata.

I had to revert this change, specifically _getting information from “PDF metadata”_.

The PDF metadata was being fetched using [pdf-reader](https://github.com/yob/pdf-reader) gem which introduced lots of its own dependencies. I just removed the dependency on pdf-reader gem for onebox.

Now the onebox will simply show pdf filename and filesize. This change significantly reduces time required to onebox because instead of fetching the whole file and loading it in memory we are now just making a HEAD request to get “Content-Length” for filesize and the URL contains filename.

Here is the demo of new PDF onebox:

[https://www.wired.com/images\_blogs/threatlevel/2009/12/redaction.pdf?new](https://www.wired.com/images_blogs/threatlevel/2009/12/redaction.pdf?new)

.

> **[Black\_Cat.pdf](https://www.ibiblio.org/ebooks/Poe/Black_Cat.pdf?new)**
>
> 80.34 KB

.

> **[Sample\_Syllabus\_for\_Wikipedia\_assignment.pdf](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sample_Syllabus_for_Wikipedia_assignment.pdf?new)**
>
> 1993.87 KB

.

> **[ruby\_tutorial.pdf](https://www.tutorialspoint.com/ruby/ruby_tutorial.pdf?2sdas)**
>
> 991.79 KB

> [@tobiaseigen](#):
>
> my PDFs do not appear to be oneboxed

I looked into this locally. It was because of pdf title not being able to forced into UTF-8 encoding. The new onebox fixes this issue:

[https://namati.org/wp-content/uploads/2017/01/4.Evidence\_Land-Rights\_-Myanmar-2017-Final.pdf](https://namati.org/wp-content/uploads/2017/01/4.Evidence_Land-Rights_-Myanmar-2017-Final.pdf)

---

<div class="post-metadata">

### 作者： ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### 发布日期： [2017 年2 月 8 日 14:08 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/7 "2017-02-08T14:08:20Z")

</div>

Fabulous. Confirmed working - thanks! 🚀

---

<div class="post-metadata">

### 作者： ![rriemann](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rriemann/32/115238_2.png) [@rriemann](https://meta.discourse.org/u/rriemann)
#### 发布日期： [2017 年2 月 9 日 17:10 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/8 "2017-02-09T17:10:44Z")

</div>

What about pdfs that are mail attachements or have been simply uploaded as attachments? They are already on the server. Wouldn’t this ease the analysis of meta-data?

---

<div class="post-metadata">

### 作者： ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### 发布日期： [2017 年2 月 9 日 17:14 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/9 "2017-02-09T17:14:03Z")

</div>

> [@rriemann](#):
>
> They are already on the server. Wouldn’t this ease the analysis of meta-data?

You still need a library to read this metadata and to load the file in memory, that’s more expensive.

---

<div class="post-metadata">

### 作者： ![rriemann](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rriemann/32/115238_2.png) [@rriemann](https://meta.discourse.org/u/rriemann)
#### 发布日期： [2017 年2 月 9 日 17:28 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/10 "2017-02-09T17:28:17Z")

</div>

I really thought that the meta-data are stored at the very beginning of the  
file, so that just a stub would need to be loaded.

---

<div class="post-metadata">

### 作者： ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### 发布日期： [2017 年2 月 9 日 17:34 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/11 "2017-02-09T17:34:24Z")

</div>

That may be correct (I am not sure), but to read the pdf file in Ruby we will have to depend on pdf-reader gem. Hence additional library and more memory.

---

<div class="post-metadata">

### 作者： ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### 发布日期： [2017 年9 月 17 日 08:40 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/12 "2017-09-17T08:40:43Z")

</div>

Are these OneBox information cached or you have to reprocess it every time the URL is shown?

If it is cached, then I can’t see why spending the time to read the file and extract the meta info should be resource-wasting.

---

<div class="post-metadata">

### 作者： ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### 发布日期： [2017 年9 月 17 日 08:47 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/13 "2017-09-17T08:47:38Z")

</div>

It’s cached – this information is baked into the HTML-version of the post 🙂

---

<div class="post-metadata">

### 作者： ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### 发布日期： [2017 年9 月 18 日 16:41 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/14 "2017-09-18T16:41:37Z")

</div>

We also have to consider the resources of install time and disk space - adding a whole bundle of other gems isn’t really helpful on that front.

---

<div class="post-metadata">

### 作者： ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)
#### 发布日期： [2017 年9 月 20 日 07:59 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/15 "2017-09-20T07:59:25Z")

</div>

> [@techAPJ](#):
>
> I had to revert this change, specifically getting information from “PDF metadata”.
> 
> The PDF metadata was being fetched using pdf-reader gem which introduced lots of its own dependencies. I just removed the dependency on pdf-reader gem for onebox.

This is a pity. Having the metadata displayed would be extremely useful, especially in the academic context, where a lot of pdfs are shared. I understand that this may not be the right setting to have enabled by default because it potentially uses a lot of resources, but is there a chance of bringing this back as a site setting? Or perhaps at least for locally uploaded pdfs, i.e. where the pdf doesn’t need to be downloaded?

---

<div class="post-metadata">

### 作者： ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### 发布日期： [2017 年9 月 20 日 15:39 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/16 "2017-09-20T15:39:21Z")

</div>

> [@tophee](#):
>
> but is there a chance of bringing this back as a site setting?

I would prefer this to, at least initially, be a plugin.

I don’t want to worry about another gem dependency, I don’t want to worry about it potentially causing memory bloat on our job processor. Putting it in a plugin a 3rd party maintains would shield us from this and allow you to nut out all the intricacies and edge cases with bad metadata that is floating around there in random PDFs.

---

<div class="post-metadata">

### 作者： ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### 发布日期： [2017 年9 月 20 日 15:46 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/17 "2017-09-20T15:46:45Z")

</div>

FWIW and after rereading my OP above, I think my need is met by the current functionality. Providing extra info about the PDF contents is a “nice to have” not a requirement.

---

<div class="post-metadata">

### 作者： ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### 发布日期： [2017 年9 月 20 日 15:58 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/18 "2017-09-20T15:58:00Z")

</div>

I don’t like this at all.

In my use case pdf attachments are directly embedded into the text. Oneboxes costs space and the real world benefit is extremely low. As I have previously suggested about a year ago, I would prefer an HTML5-based PDF viewer and more capabilities to search inside these pdf documents with the Discourse search. - Maybe, it could be nice to automaticly insert an PDF icon right before the linked file name. This signals more then enough, that an pdf file is being placed on this location

---

<div class="post-metadata">

### 作者： ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### 发布日期： [2017 年9 月 20 日 16:02 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/19 "2017-09-20T16:02:21Z")

</div>

> [@terraboss](#):
>
> Maybe, it could be nice to automaticly insert an PDF icon right before the linked file name.

I’d be happy with this too, and suggested it in the OP.

> [@tobiaseigen](#):
>
> Or perhaps even simply a file attachment appearance like you’d get using the file upload, ideally also with the file type and size provided.

But really my need here is met already and I wouldn’t want to see the discourse team devoting too much more time to making PDFs more presentable in discussions. Bike shedding and all that. But I suppose making this change could be #pr-welcome.

---

<div class="post-metadata">

### 作者： ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### 发布日期： [2017 年9 月 20 日 16:03 UTC](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972/20 "2017-09-20T16:03:46Z")

</div>

> [@terraboss](#):
>
> I would prefer an HTML5-based PDF viewer and more

That sounds like an awesome idea for a plugin! I’d guess a couple or three days of work for a programmer familiar with Discourse (which means, not me!), given that some HTML-5 based viewer already exists.

[下一页](https://meta.discourse.org/t/oneboxing-of-pdfs-and-other-attachments/55972.md?page=2)
