PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
OCR (Optical Character Recognition) is the process of converting images of typed, handwritten, or printed text into machine-encoded text.
OCR is commonly used for text recognition and extraction from the following types of documents:
The following features support OCR:
OcrLanguage constants:
| Constant | Meaning |
|---|---|
OcrLanguage::AUTO | Auto-detect language. |
OcrLanguage::ENGLISH | English (Latin). |
OcrLanguage::CHINESE | Chinese (Simplified). |
OcrLanguage::CHINESE_TRA | Chinese (Traditional). |
OcrLanguage::JAPANESE | Japanese. |
OcrLanguage::KOREAN | Korean. |
OcrLanguage::LATIN | Latin-script languages (German, French, Spanish, Portuguese, Italian, Dutch, Danish, Swedish, etc.). |
OcrLanguage::DEVANAGARI | Devanagari-script languages. |
OcrLanguage::CYRILLIC | Cyrillic-script languages. |
OcrLanguage::ARABIC | Arabic. |
OcrLanguage::TAMIL | Tamil. |
OcrLanguage::TELUGU | Telugu. |
OcrLanguage::KANNADA | Kannada. |
OcrLanguage::THAI | Thai. |
OcrLanguage::GREEK | Greek. |
OcrLanguage::ESLAV | Eastern Slavic / extended Slavic. |
In the current mainline PHP SDK, OCR languages are passed through ConvertOption::$languages for each conversion task, rather than through a separate global interface.
use ComPDFKit\Conversion\OcrLanguage;
$option = new ConvertOption();
$option->enableOcr = true;
$option->languages = [OcrLanguage::ENGLISH];
Conversion::convert('Word', 'word.pdf', 'password', 'output.docx', $option);Different OCR options can be selected according to actual needs. Below are the currently supported OCR options.
OcrOption::INVALID_CHARACTER: Recognizes invalid/garbled characters in the PDF document through OCR, while normal characters are not processed by OCR.OcrOption::SCAN_PAGE: Recognizes scanned pages in the PDF document through OCR, while editable pages are not processed by OCR.OcrOption::INVALID_CHARACTER_AND_SCAN_PAGE: Recognizes both invalid characters and scanned pages in the PDF document through OCR.OcrOption::ALL: Recognizes all pages and characters in the PDF document through OCR.When OCR is enabled, you can choose whether to enable the containPageBackgroundImage option. If this option is enabled, the original page background image of the PDF will be preserved. If it is disabled, the image result detected during page layout analysis will be retained.
The OCR function also supports converting input images into Word, Excel, Slides, HTML, CSV, RTF, TXT, JSON, and other formats. This sample demonstrates how to use the ComPDF OCR function to convert image files to a DOCX file.
$option = new ConvertOption();
// Enable OCR option.
$option->enableOcr = true;
// Set the OCR language for this task.
$option->languages = [OcrLanguage::ENGLISH];
// Supports jpg, jpeg, png, bmp, tiff, and webp formats.
Conversion::convert('Word', 'input.png', '', 'output.docx', $option);This Sample demonstrates how to use the ComPDF OCR function to convert a PDF to DOCX file.
$option = new ConvertOption();
$option->enableOcr = true;
$option->languages = [OcrLanguage::ENGLISH];
Conversion::convert('Word', 'word.pdf', 'password', 'output.docx', $option);