How to get pdf file in response for magento2 rest apiHow do I get a response from REST API in JSON format in Magento 2?How to attach an image for a product using magento2 REST API?404 Error in API responseMagento 2 Product save event with Rest APIWhat is the best way to use magento2 rest api for developing andriod an ios applicationHow to magento return raw string after calling an APIAdding extension attribute to category api responseMagento 2: Is there a way to return custom json response from REST API without building a data interface?Magento2: How to stop sending customer welcome email when order created using magento rest api?

Does the Bracer of Flying Daggers benefit from the Dueling Fighting style?

Can the druid cantrip Thorn Whip really defeat a water weird this easily?

How to discourage/prevent PCs from using door choke-points?

Confusion with the nameplate of an induction motor

Prove that the total distance is minimised (when travelling across the longest path)

Good allowance savings plan?

Decoding assembly instructions in a Game Boy disassembler

Gravity alteration as extermination tool viable?

Excess Zinc in garden soil

How to make readers know that my work has used a hidden constraint?

Am I not good enough for you?

Is all copper pipe pretty much the same?

How does Dispel Magic work against Stoneskin?

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

This equation is outside the page, how to modify it

Format picture and text with TikZ and minipage

Force user to remove USB token

Is going from continuous data to categorical always wrong?

When two POV characters meet

Is King K. Rool's down throw to up-special a true combo?

Are there situations where a child is permitted to refer to their parent by their first name?

My story is written in English, but is set in my home country. What language should I use for the dialogue?

Should QA ask requirements to developers?

validation vs test vs training accuracy, which one to compare for claiming overfit?



How to get pdf file in response for magento2 rest api


How do I get a response from REST API in JSON format in Magento 2?How to attach an image for a product using magento2 REST API?404 Error in API responseMagento 2 Product save event with Rest APIWhat is the best way to use magento2 rest api for developing andriod an ios applicationHow to magento return raw string after calling an APIAdding extension attribute to category api responseMagento 2: Is there a way to return custom json response from REST API without building a data interface?Magento2: How to stop sending customer welcome email when order created using magento rest api?













1















Currently Magento2 is providing JSON and XML response in web REST API
I want to include a custom response format PDF.



So if any third-party will call our API then we can sen them PDF in response.










share|improve this question




























    1















    Currently Magento2 is providing JSON and XML response in web REST API
    I want to include a custom response format PDF.



    So if any third-party will call our API then we can sen them PDF in response.










    share|improve this question


























      1












      1








      1








      Currently Magento2 is providing JSON and XML response in web REST API
      I want to include a custom response format PDF.



      So if any third-party will call our API then we can sen them PDF in response.










      share|improve this question
















      Currently Magento2 is providing JSON and XML response in web REST API
      I want to include a custom response format PDF.



      So if any third-party will call our API then we can sen them PDF in response.







      magento2 rest-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 21 '18 at 4:13









      Khoa TruongDinh

      21.8k64187




      21.8k64187










      asked Apr 20 '18 at 9:13









      Hitesh AgrawalHitesh Agrawal

      145




      145




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Just my idea: We shouldn't do this. We can add the download link or generated link to API response.
          There are some additional points:



          • How about your server resource if serving PDF each time calling. Your server can be stress. We should consider about this case.

          • Should consider security in this case as well.





          share|improve this answer
































            0














            First define your route in etc/webapi.xml



            <route url="/V1/invoices/:id/pdf/:template" method="GET">
            <service class="ThousandmonkeysRestpdfApiPDFInterface" method="pdf"/>
            <resources>
            <resource ref="Magento_Sales::sales" />
            </resources>
            </route>


            Then in your Model class, which you've defined in di.xml as implementing your interface (in my case ThousandmonkeysRestpdfModelPDFMaker) you can return the generated pdf easily. I've used eadesignro/module-pdfgenerator as my pdf engine (it's rather friendlier than than doing it manually).



            public function pdf($invoiceId, $templateId)
            $templateModel = $this->pdfGeneratorRepository->getById($templateId);
            if (!$templateModel)
            throw new MagentoSalesException(
            __('Could not find template.')
            );

            $invoice = $this->invoiceRepository
            ->get($invoiceId);
            if (!$invoice)
            throw new MagentoSalesException(
            __('Could not find invoice.')
            );

            $helper = $this->helper;
            $helper->setInvoice($invoice);
            $helper->setTemplate($templateModel);
            $pdfFileData = $helper->template2Pdf();
            return new PDFResponse(base64_encode($pdfFileData['filestream']));



            PDFResponse is just a class with one getter that magento uses to generate json or xml.



            It is a bit heavy so consider load, but it's a trade off between that and using the file system, which is not always a great idea (problems with load balancers & clean up). A respectful client wont cause any more issues with this than anything else. A disrespectful client could just request the pdf over and over.



            It would be a bit of a problem to open this up to customers however. You can't guarantee that they will be respectful.



            Full implementation https://github.com/lingwooc/restpdf-magento2 or just install thousandmonkeys/m2-restpdf-module






            share|improve this answer






















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "479"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f223088%2fhow-to-get-pdf-file-in-response-for-magento2-rest-api%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Just my idea: We shouldn't do this. We can add the download link or generated link to API response.
              There are some additional points:



              • How about your server resource if serving PDF each time calling. Your server can be stress. We should consider about this case.

              • Should consider security in this case as well.





              share|improve this answer





























                0














                Just my idea: We shouldn't do this. We can add the download link or generated link to API response.
                There are some additional points:



                • How about your server resource if serving PDF each time calling. Your server can be stress. We should consider about this case.

                • Should consider security in this case as well.





                share|improve this answer



























                  0












                  0








                  0







                  Just my idea: We shouldn't do this. We can add the download link or generated link to API response.
                  There are some additional points:



                  • How about your server resource if serving PDF each time calling. Your server can be stress. We should consider about this case.

                  • Should consider security in this case as well.





                  share|improve this answer















                  Just my idea: We shouldn't do this. We can add the download link or generated link to API response.
                  There are some additional points:



                  • How about your server resource if serving PDF each time calling. Your server can be stress. We should consider about this case.

                  • Should consider security in this case as well.






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 27 '18 at 1:59

























                  answered Apr 21 '18 at 4:10









                  Khoa TruongDinhKhoa TruongDinh

                  21.8k64187




                  21.8k64187























                      0














                      First define your route in etc/webapi.xml



                      <route url="/V1/invoices/:id/pdf/:template" method="GET">
                      <service class="ThousandmonkeysRestpdfApiPDFInterface" method="pdf"/>
                      <resources>
                      <resource ref="Magento_Sales::sales" />
                      </resources>
                      </route>


                      Then in your Model class, which you've defined in di.xml as implementing your interface (in my case ThousandmonkeysRestpdfModelPDFMaker) you can return the generated pdf easily. I've used eadesignro/module-pdfgenerator as my pdf engine (it's rather friendlier than than doing it manually).



                      public function pdf($invoiceId, $templateId)
                      $templateModel = $this->pdfGeneratorRepository->getById($templateId);
                      if (!$templateModel)
                      throw new MagentoSalesException(
                      __('Could not find template.')
                      );

                      $invoice = $this->invoiceRepository
                      ->get($invoiceId);
                      if (!$invoice)
                      throw new MagentoSalesException(
                      __('Could not find invoice.')
                      );

                      $helper = $this->helper;
                      $helper->setInvoice($invoice);
                      $helper->setTemplate($templateModel);
                      $pdfFileData = $helper->template2Pdf();
                      return new PDFResponse(base64_encode($pdfFileData['filestream']));



                      PDFResponse is just a class with one getter that magento uses to generate json or xml.



                      It is a bit heavy so consider load, but it's a trade off between that and using the file system, which is not always a great idea (problems with load balancers & clean up). A respectful client wont cause any more issues with this than anything else. A disrespectful client could just request the pdf over and over.



                      It would be a bit of a problem to open this up to customers however. You can't guarantee that they will be respectful.



                      Full implementation https://github.com/lingwooc/restpdf-magento2 or just install thousandmonkeys/m2-restpdf-module






                      share|improve this answer



























                        0














                        First define your route in etc/webapi.xml



                        <route url="/V1/invoices/:id/pdf/:template" method="GET">
                        <service class="ThousandmonkeysRestpdfApiPDFInterface" method="pdf"/>
                        <resources>
                        <resource ref="Magento_Sales::sales" />
                        </resources>
                        </route>


                        Then in your Model class, which you've defined in di.xml as implementing your interface (in my case ThousandmonkeysRestpdfModelPDFMaker) you can return the generated pdf easily. I've used eadesignro/module-pdfgenerator as my pdf engine (it's rather friendlier than than doing it manually).



                        public function pdf($invoiceId, $templateId)
                        $templateModel = $this->pdfGeneratorRepository->getById($templateId);
                        if (!$templateModel)
                        throw new MagentoSalesException(
                        __('Could not find template.')
                        );

                        $invoice = $this->invoiceRepository
                        ->get($invoiceId);
                        if (!$invoice)
                        throw new MagentoSalesException(
                        __('Could not find invoice.')
                        );

                        $helper = $this->helper;
                        $helper->setInvoice($invoice);
                        $helper->setTemplate($templateModel);
                        $pdfFileData = $helper->template2Pdf();
                        return new PDFResponse(base64_encode($pdfFileData['filestream']));



                        PDFResponse is just a class with one getter that magento uses to generate json or xml.



                        It is a bit heavy so consider load, but it's a trade off between that and using the file system, which is not always a great idea (problems with load balancers & clean up). A respectful client wont cause any more issues with this than anything else. A disrespectful client could just request the pdf over and over.



                        It would be a bit of a problem to open this up to customers however. You can't guarantee that they will be respectful.



                        Full implementation https://github.com/lingwooc/restpdf-magento2 or just install thousandmonkeys/m2-restpdf-module






                        share|improve this answer

























                          0












                          0








                          0







                          First define your route in etc/webapi.xml



                          <route url="/V1/invoices/:id/pdf/:template" method="GET">
                          <service class="ThousandmonkeysRestpdfApiPDFInterface" method="pdf"/>
                          <resources>
                          <resource ref="Magento_Sales::sales" />
                          </resources>
                          </route>


                          Then in your Model class, which you've defined in di.xml as implementing your interface (in my case ThousandmonkeysRestpdfModelPDFMaker) you can return the generated pdf easily. I've used eadesignro/module-pdfgenerator as my pdf engine (it's rather friendlier than than doing it manually).



                          public function pdf($invoiceId, $templateId)
                          $templateModel = $this->pdfGeneratorRepository->getById($templateId);
                          if (!$templateModel)
                          throw new MagentoSalesException(
                          __('Could not find template.')
                          );

                          $invoice = $this->invoiceRepository
                          ->get($invoiceId);
                          if (!$invoice)
                          throw new MagentoSalesException(
                          __('Could not find invoice.')
                          );

                          $helper = $this->helper;
                          $helper->setInvoice($invoice);
                          $helper->setTemplate($templateModel);
                          $pdfFileData = $helper->template2Pdf();
                          return new PDFResponse(base64_encode($pdfFileData['filestream']));



                          PDFResponse is just a class with one getter that magento uses to generate json or xml.



                          It is a bit heavy so consider load, but it's a trade off between that and using the file system, which is not always a great idea (problems with load balancers & clean up). A respectful client wont cause any more issues with this than anything else. A disrespectful client could just request the pdf over and over.



                          It would be a bit of a problem to open this up to customers however. You can't guarantee that they will be respectful.



                          Full implementation https://github.com/lingwooc/restpdf-magento2 or just install thousandmonkeys/m2-restpdf-module






                          share|improve this answer













                          First define your route in etc/webapi.xml



                          <route url="/V1/invoices/:id/pdf/:template" method="GET">
                          <service class="ThousandmonkeysRestpdfApiPDFInterface" method="pdf"/>
                          <resources>
                          <resource ref="Magento_Sales::sales" />
                          </resources>
                          </route>


                          Then in your Model class, which you've defined in di.xml as implementing your interface (in my case ThousandmonkeysRestpdfModelPDFMaker) you can return the generated pdf easily. I've used eadesignro/module-pdfgenerator as my pdf engine (it's rather friendlier than than doing it manually).



                          public function pdf($invoiceId, $templateId)
                          $templateModel = $this->pdfGeneratorRepository->getById($templateId);
                          if (!$templateModel)
                          throw new MagentoSalesException(
                          __('Could not find template.')
                          );

                          $invoice = $this->invoiceRepository
                          ->get($invoiceId);
                          if (!$invoice)
                          throw new MagentoSalesException(
                          __('Could not find invoice.')
                          );

                          $helper = $this->helper;
                          $helper->setInvoice($invoice);
                          $helper->setTemplate($templateModel);
                          $pdfFileData = $helper->template2Pdf();
                          return new PDFResponse(base64_encode($pdfFileData['filestream']));



                          PDFResponse is just a class with one getter that magento uses to generate json or xml.



                          It is a bit heavy so consider load, but it's a trade off between that and using the file system, which is not always a great idea (problems with load balancers & clean up). A respectful client wont cause any more issues with this than anything else. A disrespectful client could just request the pdf over and over.



                          It would be a bit of a problem to open this up to customers however. You can't guarantee that they will be respectful.



                          Full implementation https://github.com/lingwooc/restpdf-magento2 or just install thousandmonkeys/m2-restpdf-module







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          Chris LingwoodChris Lingwood

                          38449




                          38449



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f223088%2fhow-to-get-pdf-file-in-response-for-magento2-rest-api%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                              How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2

                              Nissan Patrol Зміст Перше покоління — 4W60 (1951-1960) | Друге покоління — 60 series (1960-1980) | Третє покоління (1980–2002) | Четверте покоління — Y60 (1987–1998) | П'яте покоління — Y61 (1997–2013) | Шосте покоління — Y62 (2010- ) | Посилання | Зноски | Навігаційне менюОфіційний український сайтТест-драйв Nissan Patrol 2010 7-го поколінняNissan PatrolКак мы тестировали Nissan Patrol 2016рвиправивши або дописавши її