attach an programmatically generated pdf file to a node
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