cooltronicpl / Craft-document-helpers

Craft CMS 3 and 4, 5 alpha, beta plugin enabling PDF document generation from template Twig files, code blocks and URLs
Other
12 stars 6 forks source link

Replace pdf when entry is updated #7

Closed arentsen closed 1 year ago

arentsen commented 2 years ago

I would like to regenerate a new pdf file automatically after the entry post has been updated (or better yet, when a specific rich text field has been updated, but it does not seem to be possible). I have experimented with the date option.

I have added {{ now.format('d/m/Y h:m') }} to the template to see if the pdf gets updated, but it does not once it has been generated for the first time. I tried with and without the pdf option date: entry.dateUpdated|date('U'). Any help would be appreciated. I don't understand what it says in the manual: date (in timestamp) default disabled, if date is provided in this parameter was smaller than file date, the file was overwwritten. Doesn't the file need to get overwritten when the entry.dateUpdated is larger than the file date, instead of smaller?

cooltronicpl commented 2 years ago

If you provide this parameter it should overwrite the PDF file. The file has its own timestamp if you provide entry.dateUpdated it should be overwritten on entry update like this fragment of code. I was using it, maybe it is not the best solution, but it overwrites a file, and it does not create PDF every time, It is an option which I tested, and it should overwrite a file on entry change or update, where there are no changes, the function is stopped by this return in code.

if (file_exists($filename) && isset($attributes['date'])) {
            if (filemtime($filename) > $attributes['date']) {
                return $filename;
            }
        }
arentsen commented 2 years ago

Thanks for that! I am using Twig templates, but this is php, so I guess I will not be able to use it?

cooltronicpl commented 2 years ago

You can test these options, you can set a timestamp if you want to include a "now" variable, but I don't know why... date: now.format('U'). If you want to have not too many writes of files (ex. you have many PDFs in a loop), you are adding date: entry.dateUpdated|date('U'), it is only when you have for example 100 PDFs for generation and you don't want to be generated every time. You can test this. The file is only overwritten when an entry is updated. I remember that configuration is working fine, and there were small writes on that server with these multiple PDF files in the loop. This is a PHP code of plugin that is responsible for a date option. This only stops function and returns the filename without generation of PDF.

cooltronicpl commented 2 years ago

I think if you provide a timestamp of now time it will be overwritten every time. Ex: date: now.format('U')

arentsen commented 1 year ago

It also seems that it overwrites it every time if I leave out the date. After upgrading to Craft 4, only overwriting an older file (after an entry changed), did not work with me. It would always serve the old pdf even though the entry changed. I just removed the date to solve this issue, but it would be better on the server if this wasn't possible...