This tutorial demonstrates how to use Aspose for Cloud to convert specified page of a PDF document to JPG image using PHP. Aspose for Cloud is a cloud-based document generation, conversion and automation platform for developers that offer a unique suite of APIs to work with Word documents, Excel spreadsheets, PowerPoint presentations, PDFs, and email formats and protocols. To run the application please edits index.php to fill the correct values of appSID and appKey. Signup at http://cloud.aspose.com for free to get these. Upload the files to a web server and it is ready to use. The server should support PHP, JSON and cURL.
Here is the code for converting specified page of a PDF document to JPG image
<?php
// Include the SDK files that we need here
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/AsposeApp.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/Product.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/Utils.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Exception/AsposeCloudException.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Storage/Folder.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Pdf/Converter.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/AsposeApp.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/Product.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Common/Utils.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Exception/AsposeCloudException.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Storage/Folder.php");
require_once(dirname(__FILE__)."/Aspose_Cloud_SDK_For_PHP/src/Aspose/Cloud/Pdf/Converter.php");
//The appSID and appKey are used for authentication
Aspose\Cloud\Common\AsposeApp::$appSID = "...";
Aspose\Cloud\Common\AsposeApp::$appKey = "...";
// The output location is the directory where we hold the converted images
// It should be have writable permissions. There should be a / at the end of this.
Aspose\Cloud\Common\AsposeApp::$outPutLocation = getcwd() . "/Output/";
Aspose\Cloud\Common\AsposeApp::$appSID = "...";
Aspose\Cloud\Common\AsposeApp::$appKey = "...";
// The output location is the directory where we hold the converted images
// It should be have writable permissions. There should be a / at the end of this.
Aspose\Cloud\Common\AsposeApp::$outPutLocation = getcwd() . "/Output/";
$input = "Sample.pdf";
$format = "jpeg";
$page = 1;
$format = "jpeg";
$page = 1;
// We shall use the Folder and Converter component of SDK
$folder = new Aspose\Cloud\Storage\Folder();
$converter = new Aspose\Cloud\Pdf\Converter($input);
$folder = new Aspose\Cloud\Storage\Folder();
$converter = new Aspose\Cloud\Pdf\Converter($input);
try {
// Upload the $input file to Aspose Cloud Storage
$folder->UploadFile($input, "");
$folder->UploadFile($input, "");
// Convert $page into specified $format and save the result in $output file
$output = $converter->ConvertToImage($page, $format);
$output = $converter->ConvertToImage($page, $format);
// Tell the web browser that the PHP script is about to send a JPG image
header("image/jpeg");
header("image/jpeg");
// Send the contents our $output file to the browser
echo file_get_contents($output);
echo file_get_contents($output);
// Delete the generated $output file. We don't need it anymore
unlink($output);
unlink($output);
// Delete the uploaded PDF too. We are all done with it.
// It is a good practice to keep the uploaded document until all operation
// are complete and the file is no more needed. Uploading the file
// again on each request will waste time :-)
$folder->DeleteFile($input);
// It is a good practice to keep the uploaded document until all operation
// are complete and the file is no more needed. Uploading the file
// again on each request will waste time :-)
$folder->DeleteFile($input);
} catch (Exception $x) {
// Ooops! let the user know what happened
echo $x->getMessage();
}
// Ooops! let the user know what happened
echo $x->getMessage();
}
Overview: Aspose for Cloud
Aspose for Cloud is a cloud-based document generation, conversion and automation platform for developers that offer a unique suite of APIs to work with Word documents, Excel spreadsheets, PowerPoint presentations, PDFs, and email formats and protocols. It supports all features for file processing, document scanning, barcodes creation and recognition, and allows extracting text or images too. You can also work with SaaSpose APIs using REST SDKs that can be called from .NET, Java, PHP and Ruby etc.
- Learn More about Aspose for Cloud
- Download complete application