I'm a bit confused with this module. Looks like I can only create a PDF file with one name.

If I keep "PDF filename" field empty, I get TCPDF ERROR: Unable to create output file: /var/aegir/platforms/drupal-8.6.0/node_108_printable_pdf.pdf -error. I suppose it has something to do with permissions. Therefore I use folder sites/default/csv which has right permissions.

With this method I have to put a fixed filename after the folder path, like this sites/default/csv/testfile.pdf.

This generates the pdf but now every page uses this same file :-(

Is there a Token support or how can I change the filename dynamically...?

CommentFileSizeAuthor
#6 3008164-6.patch2.77 KBsethhill
#3 3008164-1.patch2.44 KBaperedos

Comments

torsti created an issue. See original summary.

nigelcunningham’s picture

Category: Support request » Feature request
aperedos’s picture

StatusFileSize
new2.44 KB

This patch allow to create some tokens for filename field.

rd.michael’s picture

aperedos thanks for the patch. It almost worked for me, but I really needed the node tokens and your syntax isn't quite right for that. For "all" it shouldn't be in an array, it should be '#tokens_type' => 'all'. However, that produces a ton of possible tokens, most of which probably aren't needed. I think a better solution would be to just use the "node" tokens. i.e. '#token_types' => [ 'node' ]. This is how the Drupal 7 print module handles it too https://git.drupalcode.org/project/print/blob/7.x-2.x/print_pdf/lib_hand....

After adding that the browse available tokens link works for me as it shows the "nodes" options for token replacement. However, even after using one, such as [node:title] it doesn't get replaced in the output. I believe the context is somehow wrong, because when I use the token [current-page:title] it outputs "Printable", the title of the module, rather than the title of the front-end page. I'm not sure how to fix this issue.

rd.michael’s picture

Ah I figured it out. We need to pass in the node as context to the $token_service. If you replace line 299 of PdfFormat.php with this it will work (in addition to the above comment).

$url_parts = explode('/', $this->pathCurrent->getPath());
$node = count($url_parts) > 2 ? \Drupal\node\Entity\Node::load($url_parts[2]) : NULL;
$pdf_location = $token_service->replace($pdf_location, ['node' => $node]);
sethhill’s picture

StatusFileSize
new2.77 KB

Adding code from comment #5 into the previous patch.

nigelcunningham’s picture

Thanks for the patch, everyone - and apologies for my lack of attention to the project. I'll seek to get it in better shape, including reviewing and applying this patch.

  • NigelCunningham authored d7ab1f8 on 2.x
    Issue #3008164 by aperedos, sethhill, NigelCunningham: Tokens for file...
nigelcunningham’s picture

Status: Active » Fixed

Thanks for the work on this. I've just committed a version with some additional changes so it's not node centric. Note though that this is only a temporary file (or supposed to be) so we're mainly just avoiding collisions if multiple users request a download at the same time - the filename itself shouldn't matter too much.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.