• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating Thumbnails from PDF's

#4
[eluser]sophistry[/eluser]
i haven't actually done PDF conversion with imagemagick so no advice there.

as long as you are not in safemode or otherwise constrained, commandline apps can be called using PHP's exec() command (there are others too).

e.g., echo exec('ls') // syntax may be slightly wrong :-(

try this for sips:
Code:
$cmd = "/usr/bin/sips -s format png $pdf_file -s formatOptions high -s dpiHeight 72.0 -s dpiWidth 72.0 --out $png_file";
$exec_state = shell_exec($cmd)
it will take the first page of a PDF file and dump a png.

fpdi can extract one page at a time (using template feature) and create a new PDF file.

this should get you started. it's not beautiful but it works. if you improve it or expand it post it up here. also, you'll have to modify (or wrap) your FPDF class to accept CI-style array parameter as a constructor argument. with 60 pages, you might start to get memory errors - it may need a buffer flush in the loop where FPDI is instanced over and over.

finally, this code fragment was taken out of a controller so it assumes a few class properties that aren't specifically created here...

Code:
// sorry for the fragment, and the nasty coding style
$pdf_id = 12345;
$extension = 'pdf';
$pdf_file = $pdf_id . ".$extension";
$pdf_dir = "directoryname/$pdf_id";
// loop over the pages and suck them out
$page_num=1;
// this var is set here since we can only get its true value inside the loop
$page_count=2;
// set the orientation, units, and dimensions
$params_array = array('P','mm',array(180,270))
// load the FPDI library and pass an array of params
// class has been modified to support passing CI-style params
$this->load->library('fpdi',$params_array)
while ($page_num<=$page_count) {
    // automatically given $this->fpdi by CI,
    // but you can instantiate your own
    $this->{"fpdi".$page_num} =& new fpdi($params_array);
    
    // relative to root dir
    // talk to FPDI to get the source file and single page
    // this doesn't actually seem to return page count
    // it looks like it is asking the PDF file for it's own page count
    // but it doesn't have it in the "dictionary"
    // so we have to do a workaround and test for errors from FDPF
    $page_count = $this->{"fpdi".$page_num}->setSourceFile("$pdf_dir/$pdf_file")
    // get a filename for the new pdf
    // CI forum stripped the percent sign, sorry!
    $page_num_padded = sprintf('%04d', $page_num)
    $tplidx = $this->{"fpdi".$page_num}->ImportPage($page_num)
    
    // now talk to FPDF to add a page
    $this->{"fpdi".$page_num}->addPage()
    // tell FPDI to use the single page as template
    $this->{"fpdi".$page_num}->useTemplate($tplidx)
    
    // output the new single page as a file
    // see FPDF docs for more info on switches here
    // I = inline to browser
    //$this->fpdi->Output("newpdf.pdf","I")
    // F = save to file
    $this->{"fpdi".$page_num}->Output("$pdf_dir/$page_num_padded.$extension","F")
    $this->{"fpdi".$page_num}->closeParsers();
    
    $page_num++;
}


Messages In This Thread
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 10:25 AM
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 01:26 PM
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 02:06 PM
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 02:25 PM
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 02:41 PM
Generating Thumbnails from PDF's - by El Forum - 07-06-2007, 02:51 PM
Generating Thumbnails from PDF's - by El Forum - 07-07-2007, 07:24 PM
Generating Thumbnails from PDF's - by El Forum - 07-17-2007, 01:06 AM
Generating Thumbnails from PDF's - by El Forum - 07-17-2007, 07:40 PM
Generating Thumbnails from PDF's - by El Forum - 07-20-2007, 08:30 AM
Generating Thumbnails from PDF's - by El Forum - 07-31-2007, 04:58 AM
Generating Thumbnails from PDF's - by El Forum - 08-02-2007, 10:29 AM
Generating Thumbnails from PDF's - by El Forum - 08-28-2008, 07:26 AM
Generating Thumbnails from PDF's - by El Forum - 08-28-2008, 01:30 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.