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?
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
add a comment |
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
add a comment |
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
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
magento2 rest-api
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
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited May 27 '18 at 1:59
answered Apr 21 '18 at 4:10
Khoa TruongDinhKhoa TruongDinh
21.8k64187
21.8k64187
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 2 hours ago
Chris LingwoodChris Lingwood
38449
38449
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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