How to convert a PDF file to JPEGs using PHP

时间:2022-08-26 09:39:29

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.

How to convert a PDF file  to JPEGs  using PHP

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. &period;NET平台开源项目速览&lpar;16&rpar;C&num;写PDF文件类库PDF File Writer介绍

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

  2. C&num;写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&period;Pdf or iTextSharp or PdfSharp

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

  5. Salesforce随笔&colon; 将Visualforce Page渲染为PDF文件&lpar;Render a Visualforce Page as a PDF File&rpar;

    参照 : Visualforce Developer Guide 第60页 <Render a Visualforce Page as a PDF File> 你可以用PDF渲染服务生成一 ...

  6. Can&&num;39&semi;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&quest;

    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. sublime text 2 &plus; Dev-C&plus;&plus;&sol;MinGW 组合配置更方便快捷的 C&sol;C&plus;&plus; 编译环境(原创)

    首先看一下配置后的效果: 1.直接在底部文本框中显示运行结果(不需要从键盘输入的时候使用): 2.在cmd中运行结果(需要从键盘输入的时候使用): 快捷键说明: 运行: 在底部文本栏显示结果:Ctrl ...

  2. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  3. gulp初涉

    1.什么是gulp? gulp是前端开发过程中一种基于流的代码构建工具,是自动化项目的构建利器:它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用它,不仅可以很 ...

  4. this笔记

    在js中,如果this在全局变量和函数中this指window,在在对象中至所挂载的这个对象.

  5. Problem F&colon; 分数类的类型转换

    Description 封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:   1. 构造:传入两个参数n和m,表示n/m:分数在构造时立即转化成最简分数. 2. show()函数:分数 ...

  6. 微信小程序之图片base64解码

    不知道大家在做微信小程序的时候遇到base64解码的问题,我之前在做微信小程序的时候遇到base64解析图片一直有问题,所以在这里把遇到的问题和解决方案在这里记录一下: 在平时的项目中我们是直接用ba ...

  7. C&num;字符串比较方法

    用C#比较字符串有多种方法,如: 1. string.Compare(x,y);2. string.Equals(x,y) ; 如果要不区分大小写进行比较,则对应为:string.Compare(x, ...

  8. Qt浅谈之一:内存泄露(总结)

    一.简介       Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Q ...

  9. gcc 6&period;0编译opencv出错

    在编译opencv3.2时候,出现下面错误: cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_NE ...

  10. win2003无线网卡驱动无法安装解决方法

    Windows 2003 Server对无线网卡的pci资源分配出了问题,而笔记本bios中屏蔽了pci配置项,无法修改. 打开资源管理器菜单,工具-文件夹选项-显示,去掉“隐藏受保护的操作系统文件” ...