How to get pdf file in response for Magento 2 rest api?
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
add a comment |
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
add a comment |
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
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
magento2 rest-api pdf
edited 10 mins ago
magefms
1,477224
1,477224
asked Apr 20 '18 at 9:13
Hitesh AgrawalHitesh Agrawal
195
195
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-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
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 5 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-magento-2-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