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 ...
随机推荐
- echarts 树图
1 事件:事件绑定,事件命名统一挂载到require('echarts/config').EVENT(非模块化为echarts.config.EVENT)命名空间下,建议使用此命名空间作为事件名引用, ...
- 安装beanstalkd
运行下面命令安装: yum install epel-releaseyum -y install beanstalkd --enablerepo=epel
- ORACLE 把不是SYS用户下的所有JOB删除掉
BEGIN FOR job_id in(select job,log_user,priv_user,schema_user from dba_jobs) LOOP IF(job_id.lo ...
- flash Air 在同一个目录下面创建txt,写入字
import flash.filesystem.*; var file:File=new File(File.applicationDirectory.nativePath + '/HelloWorl ...
- ucore-lab1-练习1report
练习1 report 问题1:OS镜像文件ucore.img是如何一步一步生成的(需要比较详细地解释Makefile中的每一条相关命令和命令参数的含义,以及说明命令导致的结果)? GNU make是一 ...
- SystemC_Basic
1.http://baike.baidu.com/view/1018980.htm 百度百科介绍的很好,举例很清晰. 2.SystemC的三个基本进程:SC_METHOD,SC_THREAD,SC_C ...
- android 常用渐变背景绘制
从上到下绘制如图所示 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= ...
- Android 中Application向Activity 传递数值
比如极光注册时获取用户的唯一标示ID需要在登录时进行传递,实现消息的指定用户推送功能 public String id; public String getId() { return id; } pu ...
- js 计算丢失精度问题
计算时用parseFloat,计算完后转换成Number对象 var ytje = parseFloat("0.03"); var handlecharge = parseFloa ...
- TZOJ 3711 浪漫自习(最大流)
描述 如今的校园谈恋爱已是习以为常,这不,去上自习也要成双成对的.现在假设某班里有N对情侣从同一寝室楼出发,到达同一个教室上自习.途中,她们可能会经过长廊.静溪等一系列的景点观光游览.但情侣们不希望在 ...