How to convert a PDF file to JPEGs using PHP
Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API, which is mostly built-in in PHP installation so no need to include any thing. ImageMagick software suite allow us to create, read, edit, and compose bitmap images easily.
PHP – Convert all PDF pages to JPEG
Using following simple example you can convert all pages of PDF to JPEG images.
|
<?php
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('myfile.pdf');
// Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg
$imagick->writeImages('converted.jpg', false);
?>
|
As you are seeing, you have to pass a PDF file and it will produce JPEG files for each page of your given PDF file as output. writeImages() function second parameter is false, so it will not join the images, means it will produce image sequence(create images for each page) Example – converted-0.jpg, converted-1.jpg.

PHP – Convert specific PDF page to JPEG
If you want to convert specific page for example first page of your PDF file only then define PDF file name like this myfile.pdf[0] and run the script it will show convert only first page of your PDF file. Following is the example –
|
<?php
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('test.pdf[0]');
// Writes an image
$imagick->writeImages('converted_page_one.jpg');
?>
|
PHP – Convert specific PDF page to JPEG with quality
If you need better quality, try adding $imagick->setResolution(150, 150); before reading the file. setResolution() must be called before loading or creating an image.
|
<?php
// create Imagick object
$imagick = new Imagick();
// Sets the image resolution
$imagick->setResolution(150, 150);
// Reads image from PDF
$imagick->readImage('test.pdf[0]');
// Writes an image
$imagick->writeImages('converted_page_one.jpg');
?>
|
If you experience transparency problems when converting PDF to JPEG (black background), try flattening your file:
|
<?php
// create Imagick object
$imagick = new Imagick();
// Sets the image resolution
$imagick->setResolution(150, 150);
// Reads image from PDF
$imagick->readImage('myFile.pdf[0]');
// Merges a sequence of images
$imagick = $imagick->flattenImages();
// Writes an image
$imagick->writeImages('converted_page_one.jpg');
?>
|
shared hosting – Convert a PDF to JPEG using PHP
Most of the shared hosting providers do not compile imagick extension with PHP, but imagick binaries will be available, so here is the code to convert PDF to JPEG with imagick binaries.
|
// source PDF file
$source = "myFile.pdf";
// output file
$target = "converted.jpg";
// create a command string
exec('/usr/local/bin/convert "'.$source .'" -colorspace RGB -resize 800 "'.$target.'"', $output, $response);
echo $response ? "PDF converted to JPEG!!" : 'PDF to JPEG Conversion failed';
?>
|
You have to change binaries location (/usr/local/bin/convert) to your server location which you can get from your hosting admin.
Lots of things can be done with Imagick extension, explore more about it at – http://php.net/manual/en/book.imagick.php
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
How to convert a PDF file to JPEGs using PHP的更多相关文章
- .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍
1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...
- C#写PDF文件类库PDF File Writer介绍
.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍 阅读目录 1.PDF File Writer基本介绍 2.一个简单的使用案例 3.资源 1年前,我在文章:这 ...
- Pdf File Writer 中文应用(PDF文件编写器C#类库)
该文由小居工作室(QQ:2482052910) 翻译并提供解答支持,原文地址:Pdf File Writer 中文应用(PDF文件编写器C#类库):http://www.cnblogs.com/ ...
- create pdf file using Spire.Pdf or iTextSharp or PdfSharp
Spire.Pdf: 注:pdf 显示中文一定要设置相应的中文字体,其他外文类似.否则显示为乱码( 如果繁体的服务器上生成的中文内容PDF文档,在简体操作系统保存或并传给简体系统上查看,会存在乱码问题 ...
- Salesforce随笔: 将Visualforce Page渲染为PDF文件(Render a Visualforce Page as a PDF File)
参照 : Visualforce Developer Guide 第60页 <Render a Visualforce Page as a PDF File> 你可以用PDF渲染服务生成一 ...
- Can't create pdf file with font calibri bold 错误解决方案
错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...
- How to Convert a Class File to a Java File?
What is a programming language? Before introducing compilation and decompilation, let's briefly intr ...
- Convert 实现 pdf 和图片格式互转
pdf 转换为图片 (注意:pdf 默认转换的是透明背景,如果转为jpg格式必须添加背景色.-background white -flatten) convert -background white ...
- unable browse url when InfoPath Convert to Connection File
You must actived the windows feature "Desktop Experience" on the server : http://blogs.tec ...
随机推荐
- maven打包报错:Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
mvn package的时候报如下错误: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test ...
- 1.5.1、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包裹存储库)
使用内部包裹存储库 您可以通过托管内部存储库或手动将存储库文件复制到Cloudera Manager主机来为Cloudera Manager创建parcel存储库. 继续阅读: 托管内部包裹存储库 配 ...
- SpringJDBC数据库的基本使用
SpringJDBC的基础使用部分内容 云笔记项目数据库部分采用的是Spring-MyBatis,前面学过了JDBC,SpringJDBC,Mybatis和Spring-MyBatis,有必要重新复习 ...
- Spring mvc接收中文参数值乱码(tomcat配置问题)
问题| 使用java写的接口,中文参数乱码 问题分析| 请求方打印参数日志,中文无问题,tomcat中日志显示接收的参数乱码 实际是tomcat配置问题 解决方法| 在tomcat的配置文 ...
- Appium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks
问题: 使用Apppium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks 我的启动配置如下 { ...
- centos磁盘满了,查找大文件并清理
今天发现vps敲入crontab -e 居然提示 “Disk quota exceeded” 无法编辑.于是"df -h"查了查发现系统磁盘空间使用100%了.最后定位到是/var ...
- zabbix3.0.4安装部署与SendEmail报警配置
MySQL:5.6.21 nginx:1.62 PHP:5.7 pcre:8.32 zabbix:3.0.4 LNMP安装步骤略过 # tar xvf zabbix-3.0.4.tar.gz # cd ...
- 关于 No buffer space available (maximum connections reached?): connect 的处理
一.问题: hudson一个应用打包部署一直不成功,检查报错 检查项目的JOB配置,开始以为是SVN的问题,但是重启SVN后问题一直存在 二.分析: TCP协议中,关闭TCP连接的是Server端(当 ...
- Android 基础 (四大组件,五大存储,六大布局)
Android四大组件: 参考:https://blog.csdn.net/shenggaofei/article/details/52450668 Android四大组件分别为activity.se ...
- 通过location对象的某些属性得到一个完整URL的各个部分。
I’m home. 我回来了. I’m lost. 我迷路了. This way. 这边请. After you. 您先. Bless you! 祝福你! 笔记: http://www.example ...