joining pdf files in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Czubilinski

    #1

    joining pdf files in php

    Hello,

    I would like to join few pdf files uploaded separetly into my website
    into one downloable pdf file. Is it possible in php or is it neccessary
    to download all these files one by one?

    Thx for help,
    Paul

  • Dana Cartwright

    #2
    Re: joining pdf files in php

    "Paul Czubilinski" <pawel@deso.p l> wrote in message
    news:1139143558 .001187.267080@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > I would like to join few pdf files uploaded separetly into my website
    > into one downloable pdf file.[/color]

    I use the free PDF library called FPDF with the FPDI extension at
    fpdi.setasign.d e

    This lets you create a PDF file and then import other PDF files into it.

    The documentation for FPDF mentions two free tools to catenate PDF files,
    but I have no experience with them (see item #20 in the FAQ for FPDF).


    Comment

    • Toby Inkster

      #3
      Re: joining pdf files in php

      Paul Czubilinski wrote:
      [color=blue]
      > I would like to join few pdf files uploaded separetly into my website
      > into one downloable pdf file.[/color]

      If you've got Ghostscript installed on your server, it's easy enough to
      join a few PDFs together...

      <?php
      # Where is Ghostscript installed?
      $gs = '/usr/bin/gs';

      # Which files do we want to join together?
      # Specify them in the right order!
      $infiles = array(
      '/home/me/public_html/pdf/coverpage.pdf',
      '/home/me/public_html/pdf/part1.pdf',
      '/home/me/public_html/pdf/part2.pdf',
      '/home/me/public_html/pdf/part3.pdf',
      '/home/me/public_html/pdf/index.pdf'
      );

      # What is the output file name?
      $outfile = '/home/me/public_html/pdf/out.pdf';

      # Assemble arguments to supply to Ghostscript.
      $args = "-dBATCH -dNOPAUSE -q -sDEVICE=pdfwrit e ";
      $args .= "-sOutputFile='{$ outfile}' ";
      foreach ($infiles as $f)
      $args .= "'{$f}' ";

      # Call Ghostscript to do the real work.
      system("$gs $args");
      ?>

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • Justin Koivisto

        #4
        Re: joining pdf files in php

        Paul Czubilinski wrote:[color=blue]
        > Hello,
        >
        > I would like to join few pdf files uploaded separetly into my website
        > into one downloable pdf file. Is it possible in php or is it neccessary
        > to download all these files one by one?
        >
        > Thx for help,
        > Paul
        >[/color]

        I have a bookmark or at least example code on my computer in the office.
        I will see if I can dig it up tomorrow when I am there. I can't remember
        exactly how I went about doing it, but I think it was using an existing
        package I found on the net...

        Comment

        • Justin Koivisto

          #5
          Re: joining pdf files in php

          Justin Koivisto wrote:[color=blue]
          > Paul Czubilinski wrote:[color=green]
          >>
          >> I would like to join few pdf files uploaded separetly into my website
          >> into one downloable pdf file. Is it possible in php or is it neccessary
          >> to download all these files one by one?[/color]
          >
          > I have a bookmark or at least example code on my computer in the office.
          > I will see if I can dig it up tomorrow when I am there. I can't remember
          > exactly how I went about doing it, but I think it was using an existing
          > package I found on the net...[/color]

          fpdi:


          <?php
          // This was created for use with FPDI 1.01
          $dir = 'pdfs/';

          $pdfs = $_POST['pdfs'];
          if($pdfs) {
          // this is a test file for appending pages of PDF files together to
          create a new file
          // define('FPDF_FO NTPATH','font/');
          require 'fpdi.php';

          class concat_pdf extends fpdi {
          var $files = array();

          function concat_pdf($ori entation='P',$u nit='mm',$forma t='A4') {
          parent::fpdi($o rientation,$uni t,$format);
          }

          function set_files($file s) {
          $this->files = $files;
          }

          function concat() {
          foreach($this->files AS $file) {
          $pagecount = $this->setSourceFile( $file);
          for ($i = 1; $i <= $pagecount; $i++) {
          $tplidx = $this->ImportPage($i) ;
          $this->AddPage();
          $this->useTemplate($t plidx);
          }
          }
          }

          }

          $pdf= new concat_pdf();

          $pdf->set_files($pdf s);
          $pdf->concat();
          $pdf->Output("newpdf .pdf","I");
          } else {
          echo '<form method="post" action="index.p hp">';
          echo '<div style="padding: 3px; font-weight: bold;">Please check the
          PDFs you would like to combine into one:</div>';
          $dh = opendir($dir);
          while (false !== ($file = readdir($dh))) {
          if (preg_match('/\.pdf$/i', $file)) {
          echo '<div style="padding: 3px;"><input type="checkbox" name="pdfs[]"
          value="'.$dir.$ file.'" /> &nbsp;'.$file.' </div>';
          }
          }
          echo '<div style="padding: 3px;"><input type="submit" name="submit"
          value="Submit" /></div>';
          echo '</form>';
          }
          ?>


          I did notice that there is a newer version out now, but I haven't done
          anything with this for about a year now...

          --
          Justin Koivisto, ZCE - justin@koivi.co m

          Comment

          • Paul Czubilinski

            #6
            Re: joining pdf files in php

            Many thanks Justin, I will try your code.

            Paul

            Comment

            Working...