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 ...
随机推荐
- Java多态面试题案例几解题思路
---恢复内容开始--- Java多态面试题案例几解题思路 这道题是来自别人,先开始看到题很懵,后来有自己的思路: class A { public String show(D obj){ retur ...
- kali域名解析错误解决
浏览器出现不能上网的的现象,推测是DNS解析有问题,想要修改DNS vim /etc/resolv.conf nameserver 202.96.134.133 nameserver 114.114. ...
- jquery mobile两个页面以及源码(登录与注册) 转
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- vue element-ui怎样提炼一个自己写的js当作公共js
vue element-ui怎样提炼一个自己写的js当作公共js请教一下各位大神,我刚刚触摸vue element-ui几天,写的一个清晰检索的input框,现在需当作项目公共的部分,可遭需的html ...
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- mysql5.6 基于Binlog ROW记录方式进行数据恢复(无备份)
数据库配置注意事项 /etc/my.cnf 必须要开户binlog支持,字符集要求 是utf8 binlog类型为row server-id=121 log_bin=/home/mysqllog bi ...
- 获取txt里面的内容
import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; var txtLoad:URLL ...
- ubuntu下java的安装即使用
1.首先在官方网站(点击可以下载)下载最新的JDK,要选用self extracting installer 2.在/usr/下新建java目录,把下载的文件放到这个目录下 sudo mkdir /u ...
- Maven 标签
scope 1.compile:默认值 他表示被依赖项目需要参与当前项目的编译,还有后续的测试,运行周期也参与其中,是一个比较强的依赖.打包的时候通常需要包含进去 2.test:依赖项目仅仅参与测试相 ...
- 用webstorm开发前端项目前的一些配置
每日的开发都在紧张中进行,代码的运用和流通性大,有些想酝酿品尝的东西,来不及停留而留下遗憾,基于此,想起几次前辈们的建议,用写博客来记录这些曾在我们内心荡漾的一些东西. 需要安装的软件:WebStor ...