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的更多相关文章

  1. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  2. C#写PDF文件类库PDF File Writer介绍

    .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍   阅读目录 1.PDF File Writer基本介绍 2.一个简单的使用案例 3.资源 1年前,我在文章:这 ...

  3. Pdf File Writer 中文应用(PDF文件编写器C#类库)

    该文由小居工作室(QQ:2482052910)    翻译并提供解答支持,原文地址:Pdf File Writer 中文应用(PDF文件编写器C#类库):http://www.cnblogs.com/ ...

  4. create pdf file using Spire.Pdf or iTextSharp or PdfSharp

    Spire.Pdf: 注:pdf 显示中文一定要设置相应的中文字体,其他外文类似.否则显示为乱码( 如果繁体的服务器上生成的中文内容PDF文档,在简体操作系统保存或并传给简体系统上查看,会存在乱码问题 ...

  5. 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渲染服务生成一 ...

  6. Can't create pdf file with font calibri bold 错误解决方案

    错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...

  7. How to Convert a Class File to a Java File?

    What is a programming language? Before introducing compilation and decompilation, let's briefly intr ...

  8. Convert 实现 pdf 和图片格式互转

    pdf 转换为图片 (注意:pdf 默认转换的是透明背景,如果转为jpg格式必须添加背景色.-background white -flatten) convert -background white ...

  9. unable browse url when InfoPath Convert to Connection File

    You must actived the windows feature "Desktop Experience" on the server : http://blogs.tec ...

随机推荐

  1. DNS域名解析中A、AAAA、CNAME、MX、NS、TXT、SRV、SOA、PTR各项记录的作用

    名注册完成后首先需要做域名解析,域名解析就是把域名指向网站所在服务器的IP,让人们通过注册的域名可以访问到网站.IP地址是网络上标识服务器的数字地址,为了方便记忆,使用域名来代替IP地址.域名解析就是 ...

  2. win10自带IE上不了网的解决办法

    1.cmd以管理员身份运行powershell. 2.输入以下三条程序. netsh int tcp set heuristics disabled 回车执行后再输入 netsh int tcp se ...

  3. 初探Mybaties整合分页插件PageHelper(1)

    Mybaites整合分页PageHelper插件 在数据进行分页,通过sql语句,mysql分页,table_name表名,pageNum 第几页,pageSize 每页数据条数: SELECT * ...

  4. STOP OUR NEGATIVE THOUGHTS

    Do you ever feel like you're in over your head and at any moment you're going to burst? You're not a ...

  5. ELK 日志学习

    一.Elasticsearch 安装(所有的版本均使用5.5.0 ,且版需要相同 logstash \ kibana \ filebeat ) 官网下载地址:https://www.elastic.c ...

  6. display:none和visibility:hidden

    display:none和visibility:hidden的区别在哪儿? “这个问题简单?”我心里头暗自得意,按耐住自己得意又紧张的小心脏,自信满满地说,“这两个声明都可以让元素隐藏,不同之处在于d ...

  7. Tensorflow函数——tf.variable_scope()

    Tensorflow函数——tf.variable_scope()详解 https://blog.csdn.net/yuan0061/article/details/80576703 2018年06月 ...

  8. LeetCode第20题

    LeetCode20题不多说上代码 public boolean isValid(String s){ Stack<Character> stack = new Stack<Char ...

  9. MySQL之开启远程连接

    MySQL安装时,默认只能本地连接. mysql -u root -p mysql>use mysql; mysql>select 'host' from user where user= ...

  10. f5冗余BIG-IP系统的安装

    1.设备服务群集 •一个系列的BIG-Ips彼此互相支持DSC •每一台BIG-IP 自己生成一个Device Object •不同设备的信息 •建立信任证书 •在local device上设置Dev ...