title: Writing a Python pdfrw PdfReader object to an array of bytes / filestream
body: <p>I'm currently working on a simple proof of concept for a pdf-editor application. The example is supposed to be a simplified python script showcasing how we could use the pdfrw library to edit PDF files with forms in them.</p>
  <p>So, here's the issue. <strong>I'm not interested in writing the edited PDF to a file.</strong>
  The idea is that file opening and closing is going to most likely be handled by external code and so I want all the edits in my files to be done in memory. I don't want to write the edited filestream to a local file.</p>
  <p>Let me specify what I mean by this. I currently have a piece of code like this:</p>
  <pre class="lang-py prettyprint-override"><code>class FormFiller:
  
      def __fill_pdf__(input_pdf_filestream : bytes, data_dict : dict):
          template_pdf : pdfrw.PdfReader = pdfrw.PdfReader(input_pdf_filestream)
              # &lt;some editing magic here&gt;
          return template_pdf
  
      def fillForm(self,mapper : FieldMapper):
          value_mapping : dict = mapper.getValues()
          filled_pdf : pdfrw.PdfReader = self.__fill_pdf__(self.filesteam, value_mapping)
          #&lt;this point is crucial&gt;
  
      def __init__(self, filestream : bytes):
          self.filesteam : bytes = filestream
  </code></pre>
  <p>So, as you see the FormFiller constructor receives an array of bytes. In fact, it's an <code>io.BytesIO</code> object. The <code>template_pdf</code> variable uses a <code>PdfReader</code> object from the pdfrw library. Now, when we get to the <code>#&lt;this point is crucial&gt;</code> marker, I have a <code>filled_pdf</code> variable which is a PdfReader object. I would like to convert it to a filestream (a bytes array, or an io.BytesIO object if you will), and return it in that form. <strong>I don't want to write it to a file.</strong> However, the writer class provided by pdfrw (pdfrw.PdfWriter) does not allow for such an operation. It only provides a <code>write(&lt;filename&gt;)</code> method, which saves the PdfReader object to a pdf output file.</p>
  <p>How should I approach this? Do you recommend a workaround? Or perhaps I should use a completely different library to accomplish this?</p>
  <p>Please help :-(</p>