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

How to Pass custom parameters into PDF template. #1

Closed parwinderD4D closed 2 years ago

parwinderD4D commented 3 years ago

Hi Team, What I am trying to do we have created certificates based on entry values. I have pass entry into the template twig file as per given into documentation. All the parameters are working fine, even assigned entries as well. But I am getting errors when I try to print the date parameter under the certificate template.

Please have a look at the attached Screen-shots :

  1. http://prntscr.com/1jgxw6b [ Screen-shot show : Entry have values under dateCreated variable]
  2. http://prntscr.com/1jg29p0 [ Screen-shot show: The date parameter, I want to call into pdf file ]
  3. http://prntscr.com/1jg7gu7 [ Screen-shot show: Error on pdf template after calling dateCreated variable ]
  4. http://prntscr.com/1jgayaw [ Screen-shot show: Entry array print under pdf where dateCreated is null ]

Here is My Code

`

             {% for key, submission in submissions %}
                  {% if submission.userId == currentUser.id  %}
                  {% set srNumber = srNumber + 1 %}

        <tr>
            <th scope="row">{{ srNumber }}</th>
            <td>{{ submission.title }}</td>

            {% set pdfOptions = {
                    date: submission.dateCreated|date('U'),
                    margin_top: 10,
                    margin_bottom: 10,
                    margin_left: 1,
                    margin_right: 1,
                    mirrorMargins:1,
                } 
            %}

            <td>

                <a href="{{ alias('@web') }}/{{craft.documentHelper.pdf('_pdf/ce_certificate_template.twig', 'file', 'assets/certificates-pdf/ce-certificate-' ~ submission.dateCreated|date("Y-m-d-h:s") ~ '-' ~ random(100) ~ '.pdf' ,submission, pdfOptions)}}" download>
                    <i class="fa fa-download" aria-hidden="true"></i>
                    {{ 'Download certificate'|t }}
                </a>
            </td>
        </tr>
        {% set certificateCount = certificateCount + 1 %}
    {% endif %}

{% endfor %} 

`

Could you please help me to fix this issue?

Looking Forwards Thanks

cooltronicpl commented 3 years ago

You are using illegal characters in the name of the file. The characters are not escaped in HTML by Craft templating in the browser URL to file, try using "-" instead of ":". But I assume to insert date varaibale the best is to insert to filename the Unix timestamp of the created date: submission.dateCreated|date("U") or when the date is necessary in filename replace the : by - after hour. submission.dateCreated|date("Y-m-d-h-s") Examples: <a href="{{ alias('@web') }}/{{craft.documentHelper.pdf('_pdf/document.twig', 'file', 'pdf/' ~ submission.dateCreated|date("U") ~ '-' ~ random(100) ~ '.pdf' ,entry, pdfOptions)}}" download> <a href="{{ alias('@web') }}/{{craft.documentHelper.pdf('_pdf/document.twig', 'file', 'pdf/' ~ submission.dateCreated|date("Y-m-d-h-s") ~ '-' ~ random(100) ~ '.pdf' ,submission, pdfOptions)}}" download>

cooltronicpl commented 2 years ago

You can pass now custom variables as described in the readme from the 0.0.6 version.