Im using PDF Using MPDF module to generate 4 files when a node is saved using its API function pdf_using_mpdf_api:

function MYMODULE_node_insert($node){
        // PDF generation :
        // Generate standard version
        $view = node_view($node, 'PDF');
        $html = drupal_render($view);
        pdf_using_mpdf_api($html, 'reference_'.$node->nid);

        // Generate short version
        $view = node_view($node, 'pdf_short');
        $html = drupal_render($view);
        pdf_using_mpdf_api($html, 'reference_'.$node->nid.'_short');

        // Generate Standard EN version
        $view = node_view($node, 'pdf_en');
        $html = drupal_render($view);
        pdf_using_mpdf_api($html, 'reference_'.$node->nid.'_en');

        // Generate short EN version
        $view = node_view($node, 'pdf_short_en');
        $html = drupal_render($view);
        pdf_using_mpdf_api($html, 'reference_'.$node->nid.'_short_en');

this code above generates 4 pdf files correctly and save them under default/files/pdf_using_mpdf directory. users should be able to download these PDFs, so I added 4 fields in my content type for each PDF version (field_pdf1, field_pdf2, field_pdf3, field_pdf4).

I want to attach these stored files into their corresponding fields inside private directory (after success, i'll remove them from default/files/pdf_using_mpdf directory).

Before Im used to use field_attach_update to attach fields to a node, but for file fields how can I do this ?

What should be the value of the field : $node->field_pdf1['und'][0]['value'] ? and how can I store them into private directory ?

Thanks in advance.

3 Answers 3


I didn't use PDF Using MPDF, so not sure what will be the file path for the PDFs got generated.

Anyway, these are general steps you need to perform for attaching files to a node.

  • Create a file object representing our physical file
  • Make it a managed file by calling file_save()
  • Fill file field with file's properties and fill field's other properties like display and description
  • Save the node to make attachment effective

You can utilize sample code below to get idea how to work on it.

// We have complete file path.
$file_path = '/whatever/path/to/file/sample.pdf';

$file = new stdClass;
$file->uid = $node->uid;
$file->filename = pathinfo($file_path, PATHINFO_BASENAME);
$file->uri = $file_path;
$file->filemime = file_get_mimetype($file_path);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;

// Save file object to make it a 'managed file'.

// We will save file under this location.
$destination = 'private://PDFs/';

// Make sure destination directory exists
//before moving files to that path.
// This will create directory if it does not exists.
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);

// Now attach file to the file field.
$node->field_pdf[$node->language][0] = (array) $file;

// Default file type field comes with this extra properties.
$node->field_pdf[$node->language][0]['display'] = 1;
$node->field_pdf[$node->language][0]['description'] = 'Our special PDF';

// Save node to make the attachment effective.

Apply correct file path and destination directory. Here is the post explaining it in detail: Programmatically Attach Files to Node in Drupal 7


If I'm reading your question correctly, you'll need to

  • Move the generated PDFs to your private dir
  • Save them into Drupal's file system
  • Attach them to the $node->field_pdfX fields

See this post on how utilize the Drupal File API to save a file and attached it to a node field. All you'll need to do is substitute out the public:// prefix with private:// and reference the correct paths & field names.


I use FPDF module library to generate a PDF file object, this library is beautifull and very easy to learn, allows you to control perfect what your pdf will look, as you can see i use variable_get, set & delete, its because in library header and footer funtcions is hard to pass variables as parameters, i define them in a form, and set values on submit, here is last lines of code that does the magic:

$nombre_paciente = variable_get('paciente').'.pdf';
$np = trim($nombre_paciente);
$np = str_replace(' ', '-', strtolower($np));

$data = $pdf->Output($np,'S');
$destination = 'public://users/files/'. $pdf_node->uid . '/'.  $pdf_node->nid . '/'. $idioma;
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
// what to do when the destination file already exists
$my_file_obj = file_save_data($data, $destination .'/'. $np, $replace);
// above code saves file to server
// set node file field data
$pdf_node->field_resultado_pdf[$pdf_node->language][0] = (array) $my_file_obj;
$pdf_node->field_resultado_pdf[$pdf_node->language][0]['display'] = 1;
$pdf_node->field_resultado_pdf[$pdf_node->language][0]['description'] = 'Resultado';


$pdf->Output('D', $np, true);

it calls twice pdf->Output(), one to save file and attached to node, second to display or download PDF file, this beheaivor depends on browers, chrome downloads the file, mozilla displays it in a new tab and downlods it. My answer is based part in @Junaid answer.

enter image description here

$y = $pdf->GetY();
if($y > $page_brake){$pdf->AddPage('P', 'Letter');}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.