PHP) using Imagick to convert PDFs into pictures
Before operating, install Ghostscript on the Linux server first
650 2017-06-17 13:31:20 root CD./soft
651 2017-06-17 13:31:25 Root wget http://ghostscript.com/releases/ghostscript-8.71.tar.gz
652 2017-06-17 13:41:14 Root tar zxvf ghostscript-8.71.tar.gz
653 2017-06-17 13:41:26 Root CD ghostscript-8.71
654 2017-06-17 13:41:37 root./configure--prefix=/usr
655 2017-06-17 13:42:06 root mkdir obj
656 2017-06-17 13:42:15 Root mkdir Bin
657 2017-06-17 13:42:23 Root make all
658 2017-06-17 13:45:29 Root make Install
Then reboot:
Use Imagick;
/**
* Pdf2png
* @param $pdf pending PDF file
* @param $path The path of the picture to be saved
* @param $page the page to be exported-1 for all 0 for the first page 1 for the second page
* @return saved picture path and file name.
*/
function Pdf2png ($pdf, $path, $page =-1)
{
if (!extension_loaded (' Imagick '))
{
return false;
}
if (!file_exists ($pdf))
{
echo dirname ($pdf);
echo "File not exit";
return false;
}
$im = new Imagick ();
$im->setresolution (120,120);
$im->setcompressionquality (100);
try {
if ($page ==-1) {
$im->readimage ($pdf);
}else{
$im->readimage ($pdf. " [". $page."] ");
}
} catch (\exception $e) {
Log::notice ($e->getmessage ());
}
foreach ($im as $Key = $Var)
{
$Var->setimageformat (' png ');
$filename = $path. " /". MD5 ($Key. Time ()). PNG ';
if ($Var->writeimage ($filename) = = True)
{
$Return [] = $filename;
}
}
return $Return;
}
function Cpdf2png ()
{
$path = "Public/files";//Make sure that the folder is in the current directory, because it is always used, so it is not detected
$s =self::p df2png ("Public/files/bp/36.pdf", $path);
$scount =count ($s);
for ($i =0; $i < $scount; $i + +)
{
echo "<div Align=center><font color=red>page". ($i + 1). " </font><br><a href=\ "". $s [$i]. " \ "Target=_blank></a></div><p>";
}
}
A complete reference: http://www.jb51.net/article/60337.htm this article. Thank you so much
PHP (thinkphp) under Linux system PDF to PNG image "go"