title: How to change a texted pdf to utf-8 encoding?
body: <p>I have a bunch of pdfs that display Cyrillic properly. But if I copy and paste texts from them, gibberish was produced.</p>
  <p>Then I used the save as function from okular to convert the pdf to text file and find that the encoding is WINDOWS-1251, which is an old Cyrillic encoding. After converting it UTF-8, the Cyrillic is displayed properly.</p>
  <p>A sample link of the file is <a href="https://cdn.esis.edu.mn/cover/01/01_mongol_khel.pdf" rel="nofollow noreferrer">https://cdn.esis.edu.mn/cover/01/01_mongol_khel.pdf</a></p>
  <p><strong>Is there a way to convert the pdfs to UTF-8 encoded so that I can copy, paste and search?</strong></p>
  <hr />
  <p><strong>SOLVED</strong>:</p>
  <p>With the information provided by @iPDFdev, I managed to solve this problem.</p>
  <p>For anyone who might encounter a similar problem, I took the Windows-1251 to UTF-8 table at <a href="https://www.compart.com/en/unicode/charsets/windows-1251" rel="nofollow noreferrer">https://www.compart.com/en/unicode/charsets/windows-1251</a>, and modified the code at <a href="https://github.com/pymupdf/PyMuPDF/issues/530" rel="nofollow noreferrer">https://github.com/pymupdf/PyMuPDF/issues/530</a>. I disregarded the old Unicode map completely and added Cyrillic letters maps for all fonts on all pages.</p>
  <pre><code>import fitz
  import re
  doc = fitz.open(inputFileName)
  new = '1 beginbfrange\n&lt;c0&gt; &lt;ff&gt; &lt;0410&gt;\nendbfrange'
  for pno in range(doc.page_count):
      font_tuples = doc.get_page_fonts(2) 
      for font_tuple in font_tuples:
          for line in doc.xref_object(font_tuple[0]).splitlines():
              line = line.strip()
              if line.startswith(&quot;/ToUnicode&quot;):
                  stream_id = int(line.split()[1])
                  old_stream_decoded = doc.xref_stream(stream_id).decode()
                  new_stream_decoded = re.sub('[0-9]+? beginbfrange.*endbfrange', new, old_stream_decoded, flags=re.DOTALL)
                  new_stream_encoded = new_stream_decoded.encode()
                  doc.update_stream(stream_id, new_stream_encoded)
  doc.save(outputFileName)
  </code></pre>