我有一個應用程序,用戶可以上傳pdf
,將其轉換爲text
以供進一步處理。 事情是,一些上傳的文件是圖像PDF,所以轉換它不起作用。與其將所有pdf分成圖像然後再發送它們,我寧願只發送那些經過證明或檢測爲圖像的,有沒有辦法做到這一點,我在linux (debian)
環境中工作,php
如何檢測pdf是文本還是圖像
UPDATE
在尋找最終的解決方案,我都遵循@安德魯的建議,在生成的TXT文件計數的單詞量,如果少於10個字進行下一步:PDF格式圖片後ocr識別,這正是我現在正在處理的...
// convert any file with pdf extension to text
$cmd = "pdftotext -eol unix '$uploadedfile'";
shell_exec($cmd);
// save original file at the orig directory
rename("$uploadedfile", "orig/$uploadedfile");
// pdftotext renames files to txt so I need the file name with txt extension
$textfile = preg_replace('"\.(pdf|PDF)$"', '.txt', $uploadedfile);
// count words on the generated txt file
$cmd = "wc -w '$textfile' | cut -f1 -d' '";
$wc = shell_exec($cmd);
// proceed if words are less than 10
if ($wc < 10)
{
//take out the pdf extension for directory creation
$imgdir = preg_replace('"\.(pdf|PDF)$"', '', $uploadedfile);
$cmd = "mkdir '$imgdir'";
shell_exec($cmd);
//change pdf extension to jpg for images creation
$imgfile = preg_replace('"\.(pdf|PDF)$"', '.jpg', $uploadedfile);
//convert pdf to images
$cmd = "convert 'orig/$uploadedfile' '$imgdir/$imgfile'";
那麼它會來的OCR ...
UPDATE2 感謝的@馬克 - 瑟特查的建議,我已經改變了一點點的代碼,現在最後一部分是這樣:
//take out the pdf extension for directory creation
$imgdir = preg_replace('"\.(pdf|PDF)$"', '', $uploadedfile);
$cmd = "mkdir '$imgdir'";
shell_exec($cmd);
//convert pdf to images
$cmd = "pdfimages 'orig/$uploadedfile' '$imgdir/$imgdir'";
那麼,嘗試獲取文本。如果你的嘗試失敗,然後發送到OCR – Andrew
好的,所以我應該有辦法檢查輸出文件中是否有文本或足夠的文本?...任何建議...謝謝 –
不,你有一個應用程序「將PDF轉換成文本」就像你在問題中所說的那樣? –