attach an programmatically generated pdf file to a node 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-&gt;nid);

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

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

        // Generate short EN version
        $view = node_view($node, 'pdf_short_en');
        $html = drupal_render($view);
        pdf_using_mpdf_api($html, 'reference_'.$node-&gt;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-&gt;field_pdf1['und'][0]['value'] ? and how can I store them into private directory ?

Thanks in advance.
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.

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

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

// Save file object to make it a 'managed file'.
file_save($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-&gt;field_pdf[$node-&gt;language][0] = (array) $file;

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

// Save node to make the attachment effective.
node_save($node);


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