How to get pdf file in response for Magento 2 rest api?How do I get a response from REST API in JSON format...

Making a sword in the stone, in a medieval world without magic

Ban on all campaign finance?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Humans have energy, but not water. What happens?

Counter-example to the existence of left Bousfield localization of combinatorial model category

Am I not good enough for you?

Straight line with arrows and dots

Life insurance that covers only simultaneous/dual deaths

Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?

Potentiometer like component

Decoding assembly instructions in a Game Boy disassembler

Welcoming 2019 Pi day: How to draw the letter π?

Is "history" a male-biased word ("his+story")?

Silly Sally's Movie

How could a female member of a species produce eggs unto death?

Why do Australian milk farmers need to protest supermarkets' milk price?

When two POV characters meet

Confusion with the nameplate of an induction motor

What exactly is the purpose of connection links straped between the rocket and the launch pad

Playing ONE triplet (not three)

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

Make a transparent 448*448 image

What is the likely impact on flights of grounding an entire aircraft series?

It's a yearly task, alright



How to get pdf file in response for Magento 2 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?













2















Currently Magento 2 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





























    2















    Currently Magento 2 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



























      2












      2








      2








      Currently Magento 2 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 Magento 2 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 pdf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 min ago









      magefms

      1,477224




      1,477224










      asked Apr 20 '18 at 9:13









      Hitesh AgrawalHitesh Agrawal

      195




      195






















          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-magento-2-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 5 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-magento-2-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

                              “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                              How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                              夢乃愛華...