Announcement

Collapse
No announcement yet.

[API] Download PDF from templates after entity creation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [API] Download PDF from templates after entity creation

    Hello,
    Working for an insurance customer, who generates prospects on its website, prospects are sent and properly stored in ESPO right now (via API).
    From this newly created PROSPECT entity, several PDF are generated from PDF templates (custom quote, custom conditions, etc.). they are accessible on the espocrm customer portal.

    I'm trying to retrieve via API the custom generated PDF files linked to this entity.
    I don't see any doc related to that (limited doc on PDF templates, no doc on fetching generated PDF via API)

    Could someone point me in the right direction ?

    Thanks in advance
    M



  • #2
    You could share this PDF through entryPoint

    Comment


    • #3
      That seems like overkill in terms of development ?

      I thought about it. Is there any way to directly (API or URL) access the PDF files generated through PDF templates ?

      Comment


      • #4
        Okay, I've found a way

        When the PDF are generated (to be sent attached to the customer email) :
        $customconditionsPDF_ID= ext\pdf\generate(
        'Entity', entity\attribute('id'), 'pdftemplateid', 'filename.pdf') ;
        an attachment is actually created with an ID ( $customconditionsPDF_ID ).
        I will store this ID in the Entity, then the website can fetch the entity, get the attachements ID(s) and fetch the attachments to be displayed on the website.


        Comment


        • #5
          "That seems like overkill in terms of development ?"
          They're actually shared through entrypoint BY DEFAULT :P
          If you try to generate pdf from any record, you'll see that you're getting access to generated PDF through entryPoint

          Comment


          • #6
            Okay, so I missed your point
            what do you mean by "through entryPoint" ? I am sure I don't get the generated PDF when I just fetch the Entity

            Comment


            • #7
              Malaiac i mean that when you click Print to PDF, your browser will show you generated PDF in new tab.
              And if you check link, you'll see that it's entryPoint.

              https://demo.espocrm.com/?entryPoint=pdf&entityType=Quote&entityId=55eee1049226ba2a5&t emplateId=001

              Comment


              • #8
                Originally posted by emillod View Post
                Malaiac i mean that when you click Print to PDF, your browser will show you generated PDF in new tab.
                And if you check link, you'll see that it's entryPoint.

                https://demo.espocrm.com/?entryPoint=pdf&entityType=Quote&entityId=55eee1049226ba2a5&t emplateId=001
                Well, I know how to get PDF files through the espocrm GUI.
                The URL is https://espocrm. [customer] .com/?entryPoint=pdf&entityType=[entityName]&entityId=6012a10757fa28e33&templateId=5ff08785 431 f7ae10
                This of course only works when you're logged in espocrm.[customer].com

                What I'm working on is getting the PDF files via the API.
                Right now, I'm using the active workflow to store the PDF ids. ( workflow generate the 2 PDFs and send them to the lead email)
                So I have a quote_id and conditions_id in the Entity row, they look like 6012a2d07ded5fe3c
                I can get both those ids via API ( get Entity ), call is :
                https://espocrm.[customer].com/api/v1/Attachment/file/6012a2d07ded5fe3c
                I do get a full response, but the data part is not proper PDF
                ( it does start with :

                %PDF-1.7 %���� 7 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20210128114103+00'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /PZ 1 >> endobj

                but won't save as a readable PDF
                )

                Comment


                • #9
                  Maybe i'm missing something but i don't understand what you want achieve. We've created script which is saving generated PDF by EspoCRM and adding this PDF as attachment to record. We've also created entryPoint which is available for users outside EspoCRM.

                  Comment


                  • #10
                    What I want to achieve is to get links on the website that allows the website visitor to download PDF generated by ESPO.


                    The PDF is generated when the entity is created

                    $quoteid = ext\pdf\generate(
                    '<EntityName>',
                    entity\attribute('id'),
                    '<templateid>',
                    string\concatenate(name,'_QUOTE_',cpnumber,)) ;

                    then the PDF id saved to the Entity
                    entity\setAttribute( 'quoteID', $quoteID);

                    (then sent to the lead email)

                    From the customer website I can then get the Entity with the quoteID attribute ( value is something like '6012a2d07ded5fe3c' )
                    And then call via API the /api/v1/Attachment/file/6012a2d07ded5fe3c

                    The API response seems ok but :
                    - the data size is 177kB while the supposedly same PDF downloaded from the ESPO GUI is 280kB
                    - I'm unable to save the data string as a proper PDF file (whether by direct download or by saving it to the website folder)

                    Comment


                    • #11
                      I'm not sure is that a good approach to download it through API.
                      I think better approach is through EntryPoint. Here you have free version of extension which will let you to share with your attachments outside EspoCRM https://github.com/dubas-pro/ext-public-attachment

                      We have premium version which will let you to share only specific files through EntryPoint only in specific time and based on safe URL.
                      I'm not sure what do you mean customer website, is it a external website or EspoCRM portal, but if it's something external you could generate unique URL for download specific file based on user action and then user could download file thorugh this link.

                      Comment


                      • #12
                        Following on this question.
                        I downloaded the the ext-public-attachment extension, changed the allowedFileTypes to 'application/pdf' , then installed it onto the espocrm.<client>.com portal.

                        There is a file ID 602662880xxxx
                        I tried
                        https://espocrm.<client>.com/#?entryPoint=DubasPublicAttachment&id=602662880xxx x
                        without success ( > redirect to the login page )

                        The goal is still the same: allow a visitor on the client website to download PDF files generated by ESPO

                        Right now, I cannot download the PDF via API ( wrong file, too short). and I cannot give direct access to the PDF :/

                        Comment


                        • #13
                          Malaiac i don't know.. I've shared with community simple extension which will be available for few more week. After that we'll release our paid extension which will be more complex.

                          Comment


                          • #14
                            Solved by https://github.com/espocrm/espocrm/issues/1922

                            Comment


                            • esforim
                              esforim commented
                              Editing a comment
                              Look good, another coder joining the contribution. Welcome to EspoCRM forum
                          Working...
                          X