# Output: how to save a field to a pdf file?

**URL:** https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618
**Category:** Logstash
**Created:** [December 12, 2019, 10:38am UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618 "2019-12-12T10:38:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [December 12, 2019, 10:38am UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618/1 "2019-12-12T10:38:50Z")

</div>

Hi there

I'm using logstash ([docker.elastic.co/logstash/logstash:7.4.0](http://docker.elastic.co/logstash/logstash:7.4.0)) to ingest data to our open data portal and saving said data to files (.csv, .geojson, etc.).

All was well, until we got to one of our data sources which contains a pdf file, encoded in base64, in one of the fields. We get this data through an API and the pdf data is automagicaly ingested into a field (`json` codec in our `input{}`) as expected (thank you logstash!).

I need to save the content of this field to an actual pdf file. In order to do so I've tried to use the `file{}` output plugin, after decoding the data, like this:

```auto
filter {
  ruby {
    code => "
      event.set('pdf_data', Base64.decode64(event.get('json_field_with_b64_data')))
    "
  }
}

output {
  file {
    path => "data/%{my_file_name}.pdf"
    codec => plain {format => "%{pdfdata}"}
  }
}

```

What I get is a `.pdf` file with, apparently, the right contents (if I open it with a text editor), but which turns to a blank page when I open it with Acrobat Reader. I have also tried with `line` codec instead of `plain`, with the same result.

I've been searching the documentation looking for other output plugins, and other codecs, but I haven't been able to find an alternative. I have not found any similar case here either.

Maybe a ruby filter could do the writing instead? Has anyone done something similar? Is it even possible to write a pdf file from logstash? Any ideas or pointers will be appreciated.

Thanks in advance

---

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [December 17, 2019, 12:56pm UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618/2 "2019-12-17T12:56:53Z")

</div>

Hi,

I'm still struggling with this. I'd really appreciate it if someone could provide some help.

Thank you

---

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [January 8, 2020, 8:11am UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618/3 "2020-01-08T08:11:17Z")

</div>

Hi

Can someone help me with this? I´m trying to write pdf data to a pdf file from within logstash.

Thank you.

---

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [January 20, 2020, 12:39pm UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618/4 "2020-01-20T12:39:09Z")

</div>

Hi

I solved it. I moved the writing from the `output{}` to the `filter{}`, as shown in the code at the end of this: [https://dev.to/gadimbaylisahil/turn-base64-images-into-pdf-in-ruby-with-prawn-3o04](https://dev.to/gadimbaylisahil/turn-base64-images-into-pdf-in-ruby-with-prawn-3o04).

The code, if anyone is interested, looks like this (names changed to protect the innocent):

```auto
ruby {
      init => "require 'base64'"
      code => "
        path_pdf = 'data/' + event.get('my_file_name.pdf')
        File.open path_pdf, 'wb' do |file|
          file.write Base64.decode64(event.get('json_field_with_b64_data'))
          file.close
        end
      "
      remove_field => ["json_field_with_b64_data"]
    } 

```

This writes a .pdf file with the right contents that can be open with Adobe Acrobat Reader with no issues.

Hope this can help someone in the future.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 17, 2020, 12:39pm UTC](https://discuss.elastic.co/t/output-how-to-save-a-field-to-a-pdf-file/211618/5 "2020-02-17T12:39:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
