How to save pdf file in public files directory in drupal 7

Sometimes we need to save pdf file in directory. In drupal for this purpose we can use print-email-pdf module wich give us pdf file when we follow url  yoursite.com/printpdf/nid. We can easily grab the content of this file using file_get_contents() function. You should look at code example:

$date = date('d-m-Y');
    $file_path  = variable_get('file_public_path', conf_path() . '/files/webform');
    $path = $_SERVER['DOCUMENT_ROOT'] . base_path() . $file_path . '/' . $date;
    //here we will create directory to store order files
    if(file_prepare_directory($path,  FILE_CREATE_DIRECTORY)){
      global $base_root;
      $test =  $base_root . '/printpdf/' . $node->nid;
      $pdf = file_get_contents($test);
      $path .= '/' . $node->nid;
      if(!empty($pdf) && file_prepare_directory($path,  FILE_CREATE_DIRECTORY)){
        $path .= '/order.pdf';
        //we save pdf file into order dir
        file_put_contents($path, $pdf);
      }
    }
Rating: 
6.4 out of 10 based on 5 ratings.
Tags: