Programming

PHP에서 PDF 문서를 미리보기 이미지로 어떻게 변환합니까?

procodes 2020. 5. 13. 20:52
반응형

PHP에서 PDF 문서를 미리보기 이미지로 어떻게 변환합니까? [닫은]


PDF 문서의 일부를 이미지 파일로 렌더링하려면 어떤 라이브러리, 확장자 등이 필요합니까?

내가 찾은 대부분의 PHP PDF 라이브러리는 PDF 문서 작성을 중심으로하지만 웹 사용에 적합한 이미지 형식으로 문서를 렌더링하는 간단한 방법이 있습니까?

우리 환경은 LAMP 스택입니다.


당신은 필요 ImageMagick하고GhostScript

<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

[0]수단 page 1.


어떤 이유로 든 ImageMagick이없는 사람들을 위해 GD 기능도 GhostScript와 함께 작동합니다. ghostscript 명령을 실행하여 exec()PDF를 JPG로 변환 하고을 사용하여 결과 파일을 조작하십시오 imagecreatefromjpeg().

고스트 스크립트 명령을 실행하십시오.

exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=whatever.jpg input.pdf')

조작하려면 새 자리 표시 자 이미지를 만들고 $newimage = imagecreatetruecolor(...)현재 이미지를 가져옵니다. $image = imagecreatefromjpeg('whatever.jpg')을 사용 imagecopyresampled()하여 크기 또는 다른 내장 imagemagick명령이 아닌 수를 변경할 수 있습니다.


다음을 사용하여 페이지 수를 얻을 수도 있습니다.

$im->getNumberImages();

그런 다음 루프를 사용하여 모든 페이지의 엄지 손가락을 만들 수 있습니다.

'file.pdf['.$x.']'

PHP 확장명 Imagick을 사용하십시오 . 래스터 출력 이미지의 원하는 크기를 제어하려면 setResolution 기능을 사용하십시오.

<?php    
$im = new Imagick();
$im->setResolution(300, 300);     //set the resolution of the resulting jpg
$im->readImage('file.pdf[0]');    //[0] for the first page
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

(Paolo Bergantino는 그의 답변을 확장했으며 Luis Melgratti는 그의 의견을 확장했습니다. 이미지를로드하기 전에 해상도를 설정해야합니다.)


Blob에서 PDF를로드하는 경우 마지막 페이지 대신 첫 페이지를 얻는 방법입니다.

$im->readimageblob($blob);
$im->setiteratorindex(0);

imagemagick과 함께 제공되는 'convert'유틸리티를 실행할 수도 있습니다.

exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';

나는 PHPlibGhostScript 래퍼 PDFlib 의 저자입니다. 이 라이브러리를 사용하는 이점은 이미 테스트되었으며 필요하지 않습니다.ImageMagic

항상 PDF에 GhostScript비해 명령이 더 빠르 ImageMagic므로 GhostScript 래퍼 또는 순수한 GhostScript 명령을 사용해야합니다.

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();

설치 완료했습니다! 작동했습니다!

Windows에서 imagemagick 기본 설치를 할 수 있습니다 .

에서 php (local)사용하는 호출 exec(<command line>)예 :

<?php
$pdf = "filename.pdf";
$info = pathinfo($pdf);
$file_name =  basename($pdf,'.'.$info['extension']);
echo $file_name;
$pdf = "filename.pdf[0]";
exec("convert $pdf convert-img/$file_name.jpg");    
?>

게다가, 당신은 PHP Imagick 클래스class imagick 에서 사용할 수 있습니다

모두 도와 주셔서 감사합니다!


Here is a simple class I've written and used on a couple of projects. It just wraps imagick and handles writing each page out to disk. If anyone is still looking for an easy way to do this, this link might be helpful.


Think differently, You can use the following library to convert pdf to image using javascript

http://usefulangle.com/post/24/pdf-to-jpeg-png-with-pdfjs

참고URL : https://stackoverflow.com/questions/467793/how-do-i-convert-a-pdf-document-to-a-preview-image-in-php

반응형