Text selection is not transparent in PDFs OCRed with Tesseract

In such PDFs the selected text becomes hidden behind the selection; see this issue, which also describes the workaround in detail. The following function, which depends on the qpdf.el package, can be used to convert such a PDF file into one where text selection is transparent:

(defun my-fix-pdf-selection ()
  "Replace pdf with one where selection shows transparently."
  (unless (equal (file-name-extension (buffer-file-name)) "pdf")
    (error "Buffer should visit a pdf file."))
  (unless (equal major-mode 'pdf-view-mode)
  ;; save file in QDF-mode
  (qpdf-run (list
             (concat "--infile="
             "--qdf --object-streams=disable"
  ;; do replacements
  (read-only-mode -1)
  (while (re-search-forward "3 Tr" nil t)
    (replace-match "7 Tr" nil nil))

Note that this overwrites the PDF file visited in the buffer from which it is run! To avoid this replace the --replace-input with (concat "--outfile=" (file-truename (read-file-name "Outfile: "))).