Riddex
Riddex

Reputation: 21

How can I rotate a pdf's content with PHP on a windows machine?

On the site we have clients uploading pdf files, in the majority of the cases they are in the correct orientation.There are some in landscape, we would like to be able to rotate content and save in the admin interface.

I am looking for the easiest, and cheapest option to do rotations of pdf content across all the pages within the uploaded document.

I have looked at pdflib, but having issues getting the lite version to compile and the licences for the product are mega expensive.

The site runs in a WAMP configuration.

Upvotes: 2

Views: 813

Answers (2)

Jan Slabon
Jan Slabon

Reputation: 5058

We offer a library (not free!) allowing this without any external programm but in pure PHP:

<?php
// your variables
$degrees = 90;
$filename = "your.pdf";

//require autoloader
require_once("library/SetaPDF/Autoload.php");

// create a file writer
$writer = new SetaPDF_Core_Writer_File("rotated.pdf");
// load document by filename
$document = SetaPDF_Core_Document::loadByFilename($filename, $writer);

// get pages object
$pages = $document->getCatalog()->getPages();
// get page count
$pageCount = $pages->count();

for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
    // get page object for this page
    $page = $pages->getPage($pageNumber);

    // rotate by...
    $page->rotateBy($degrees);
}

// save and finish the document
$document->save()->finish();

An online demo is available here, too.

Upvotes: 1

Marcin Wasilewski
Marcin Wasilewski

Reputation: 735

You can use Imagick library

$imagick = new Imagick(); 
$imagick->readImage('landscape.pdf'); 
$angle = 90; //or -90 if you want anti-clockwise
$imagick->rotateimage(new ImagickPixel(), $angle); 
$imagick->setImageFormat("pdf"); // that's likely not necessary
$imagick->writeImage("portrait.pdf");

I'm sure there must be some command line converters for windows so you have a second option of running a 3rd party program using exec(). But if performance is not an issue then Imagick will probably do the trick.

Upvotes: 1

Related Questions