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. java+selenium自动化实践

    git+java+selenium+testng +maven+idea 1.git之代码维护(下载.分支切换.上传) 下载命令 "git clone git@github.com:Luna ...

  2. Android LayoutInflater 相关知识

    今天看鸿洋大神公众号, 换肤.全局字体替换.无需编写shape.selector 的原理Factory小结 自己写代码时也要多思考,多问自己.

  3. spingboot @EnableScheduling

    springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以. @EnableScheduling 无 ...

  4. MVC004之页面引用命名空间

    描述:在控制器中返回了一个ViewData["currUser"] 给view,ViewData["currUser"]为 Greatwall.Hr.Bll.H ...

  5. Struts学习资料

    Strust组件—ActionServlet详解 http://ltc603.iteye.com/blog/68637

  6. sql去除重复语句

    转自芙蓉清秀的BLOG http://blog.sina.com.cn/liurongxiu1211  sql去除重复语句 (2012-06-15 15:00:01) sql 单表/多表查询去除重复记 ...

  7. PAT1021(dfs 连通分量)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  8. [剑指Offer]50-第一个只出现一次的字符

    题目链接 https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&t ...

  9. [Java学习]面向对象-多态

    多态 多态发生条件 发生在有继承关系的类型中. 向上转型(自动类型转换)与向下转型(强制类型转换) //向上转型 //编译阶段a1被编译器看作是Animal类型,所以a1引用绑定的是Animal类中的 ...

  10. java测试感受

    这个星期四下午来了一次Java考试,用来测试在暑假自学Java的学习情况,不得不说这次考试十分的成功,把我对这学期的学习信心打击的很难受,我也知道这是我应得的教训,我也对我的专业水平有了很深刻的了解了 ...