0

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.

2

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.

<?php
// 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'.
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->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.
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

1
1

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.

1

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.