The issue you're experiencing with your Russian PDF is a common encoding problem. When you copy text from the PDF, it appears as "Àíãëèéñêèé ÿçûê" instead of proper Cyrillic characters because the text is encoded in Windows-1251 (Cyrillic) but is being interpreted as Windows-1252 (Western European).
Understanding the Problem
This is a classic encoding mismatch. Your PDF contains text in Windows-1251 encoding (used for Cyrillic characters), but when you copy it, the system is interpreting it as Windows-1252 encoding (used for Western European languages)[1][6]. This results in garbled text that looks like Latin characters with accents instead of proper Russian text.
Solutions to Convert the Encoding
Here are several approaches to solve this problem:
Using iconv (Command Line)
The iconv utility is excellent for encoding conversion tasks:
iconv -f WINDOWS-1251 -t UTF-8 input.txt > output.txt
For PDF files, you'll need to extract the text first, then convert it[3][7].
Batch Conversion Script
For automating the process, you can use this bash script to convert multiple files:
#!/bin/bash
# Recursive file conversion from windows-1251 to utf-8
find ./ -name "*.txt" -type f |
while read file
do
echo "Converting $file"
mv "$file" "$file.icv"
iconv -f WINDOWS-1251 -t UTF-8 "$file.icv" > "$file"
rm -f "$file.icv"
done
This script finds all text files in the current directory and its subdirectories, then converts them from Windows-1251 to UTF-8[7].
Improved Script with Encoding Detection
To avoid converting files that are already in UTF-8:
#!/bin/bash
find ./ -name "*.txt" -type f |
while read file
do
if ! file -bi "$file" | grep -q 'utf-8'
then
echo "Converting $file"
mv "$file" "$file.icv"
iconv -f WINDOWS-1251 -t UTF-8 "$file.icv" > "$file"
rm -f "$file.icv"
fi
done
This script checks the encoding before conversion to avoid double-converting files[7].
Using enca for Automatic Detection
If you're unsure about the encoding, enca can automatically detect and convert:
find ./ -name "*.txt" -type f | while read file; do enca -L russian -x UTF-8 "$file"; done
This is particularly useful for Russian filenames with spaces[7].
PDF-Specific Solutions
For PDFs, the process is more complex since PDFs handle text differently:
Using Adobe Acrobat
If you have Adobe Acrobat (not just Reader), you can try:
- Open the PDF
- Go to File > Save As > More Options > PDF/A
- This might normalize the text encoding[8]
Using Google Chrome or Google Docs
A simple workaround:
- Open the PDF in Google Chrome (which has a built-in PDF reader)
- Copy text from there - Chrome often handles encoding issues better than dedicated PDF readers[10]
- Alternatively, try opening the PDF with Google Docs, which can sometimes interpret the encoding correctly[10]
PDF Text Extraction and Conversion
For automation:
- Extract text from PDF using a tool like
pdftotext
- Convert the extracted text using iconv
- If needed, create a new PDF with the properly encoded text
Python Solution
If you prefer Python, here's a script that can help:
import os
import glob
import chardet
from bs4 import BeautifulSoup
for file in glob.glob("*.txt"): # Change to extract from PDF first
# Detect encoding
rawdata = open(file, "rb").read()
encoding = chardet.detect(rawdata)['encoding']
if encoding in ["Windows-1251", "ascii", "ISO-8859-1"]:
print("Converting", file)
# Convert to utf8
data = open(file, "r", encoding=encoding).read()
with open(file, 'w', encoding='utf-8') as outp:
outp.write(data)
This script detects the encoding of each file and converts it to UTF-8[4].
Root Cause Analysis
The fundamental issue is that PDFs can use custom font encodings. When you check the PDF properties (File > Properties > Fonts tab), you might see that the encoding is listed as "custom" or something other than standard UTF-8[10]. This explains why the text displays correctly in the PDF viewer but becomes garbled when copied.
The most reliable solution is to fix the encoding at the source when creating the PDF, but the methods above should help you work with existing files.