Saving a view as an file/image

74 views
Skip to first unread message

John Bannister

unread,
Jan 26, 2020, 9:25:47 AM1/26/20
to web2py-users
Hi All,

I have hit a bit of a stumbling block and would like to know if anyone else has come across this and how to resolve it.

Situation is as follows:-

I am generating tickets/coupons for users once they have entered all the appropriate info which I am doing via normal controller/view using html for styling. Coupon consists of 2 images plus some text (nothing really fancy at all).

Everything looks okay and as it should in the view with all the correct information etc.

I now what to email the coupon to the user. This is where I am a bit stumped.

I am assuming that I will need to save the rendered view as an image/file/.pdf if I want the styling, logos, qrcodes etc to be in the email that I will send as well as for all the formatting to be as per the view.

Is there any easy way or any way at all to do this?

In the controller I can use the response.render(view, args) but still cant see how to create a pdf file from the actual view before sending the email.
Hopefilly I ma missing something simple  

Thanks in advance 

Scott Hunter

unread,
Jan 26, 2020, 2:04:43 PM1/26/20
to web2py-users
The FPDF module has an HTML Renderer which you might find useful.

- Scott

villas

unread,
Jan 27, 2020, 7:25:48 AM1/27/20
to web2py-users
Generating PDFs can be a challenge, so it would be much easier to style the vouchers in HTML.

Re: QRcodes.  You can even generate QRcodes using Markmin, or, just use the Google Charts API directly. Search web for other solutions.

António Ramos

unread,
Jan 27, 2020, 9:05:28 AM1/27/20
to web...@googlegroups.com
for pdf generation this is awesome!!! and has example with web2py 

setup for web2py ...


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/fc768134-c453-4b94-838b-f4ee96d932bc%40googlegroups.com.

mostwanted

unread,
Jan 27, 2020, 9:47:09 AM1/27/20
to web...@googlegroups.com
Hey John, I dont know if this will help you but there was a time when i needed to convert a view to PDF & I used 2 l js libraries, html2canvas & jsPDF, they enabled me to achieve my goal that time, you can find the jsPDF package here: https://github.com/MrRio/jsPDF
and the html2canvas package here: https://github.com/niklasvh/html2canvas/releases
After extracting all the files from the compressed folders look for these 2 js files in jsPDF;  jspdf.debug.js, jspdf.min.js, and the html2canvas.js in the html2pdf folder, upload them into your application's static/js directory, from there call them in your application's layout page like this:
<script src="{{=URL('static','js/jspdf.debug.js')}}"></script>
<script src="{{=URL('static','js/jspdf.min.js')}}"></script>
<script src="{{=URL('static','js/html2canvas.js')}}"></script>

In the view that you want to convert to PDF have this javascript code:
<script type="text/javascript">
   
function genPDF()
   
{
        let pdfName
= "licencia";
var doc = new jsPDF({
format
: "a4"
});

  html2canvas
(document.getElementById("IDName"), {
    scale
: "5"
 
}).then(canvas => {
    console
.log("Capturando");
   
this.imgFile = canvas;
    doc
.addImage(this.imgFile, "JPEG", 5, 5, 200, 250);
    doc
.save('filename.pdf');
 
});
   
}
</script>

In the same view have a link that when clicked calls the above js function like this:
<div id="pdf"><a href="javascript:genPDF()">Click to Download as PDF</a></div>

This should convert you view into PDF or Image as per the usage of both liraries.

I hope the maintainers of the jsPDF & html2pdf libraries have not made adjustmets that may cause this not to work;

Regards

Mostwanted

P:S; I'm curious, how did you generate QRCodes in your application?

John Bannister

unread,
Jan 28, 2020, 3:52:11 AM1/28/20
to web...@googlegroups.com

I have no issue styling the vouchers in HTML  (this is already done in a view) my issue  is how to capture that view and send it via email either as message or attachment.

--

Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

John Bannister

unread,
Jan 28, 2020, 3:54:46 AM1/28/20
to web...@googlegroups.com

Hey thanks a lot will try it out!!

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of mostwanted
Sent: 27 January 2020 15:47
To: web2py-users
Subject: [web2py] Re: Saving a view as an file/image

 

Hey John, I dont know if this will help you but there was a time when i needed to convert a view to PDF & I used 2 l js libraries, html2canvas & jsPDF, they enabled me to achieve my goal that time, you can find the jsPDF package here: https://github.com/MrRio/jsPDF

and the html2canvas package here: https://github.com/niklasvh/html2canvas/releases

After extracting all the files from the compressed folders look for these 2 js files in jsPDF;  jspdf.debug.js, jspdf.min.js, and the html2canvas.js in the html2pdf folder, upload them into your application's static/js directory, from there call them in your application's layout page like this:

<script src="{{=URL('static','js/jspdf.debug.js')}}"></script>
<script src="{{=URL('static','js/jspdf.min.js')}}"></script>
<script src="{{=URL('static','js/html2canvas.js')}}"></script>

<script src="{{=URL('static', 'js/moments.js')}}"></script>

 

In the view that you want to convert to PDF have this javascript code:

<script type="text/javascript">
   
function genPDF()
   
{
        let pdfName
= "licencia";
var doc = new jsPDF({
format
: "a4"
});



  html2canvas
(document.getElementById("invoicing"), {


    scale
: "5"
 
}).then(canvas => {
    console
.log("Capturando");
   
this.imgFile = canvas;
    doc
.addImage(this.imgFile, "JPEG", 5, 5, 200, 250);
    doc
.save('filename.pdf');
 
});
   
}
</script>

 

In the same view have a link that when clicked calls the above js function like this:

<div id="pdf"><a href="javascript:genPDF()">Click to Download as PDF</a></div>


This should convert you view into PDF or Image as per the usage of both liraries.

 

I hope the maintainers of the jsPDF & html2pdf libraries have not made adjustmets that may cause this not to work;

 

Regards

 

Mostwanted


On Sunday, January 26, 2020 at 4:25:47 PM UTC+2, John Bannister wrote:

Hi All,

 

I have hit a bit of a stumbling block and would like to know if anyone else has come across this and how to resolve it.

 

Situation is as follows:-

 

I am generating tickets/coupons for users once they have entered all the appropriate info which I am doing via normal controller/view using html for styling. Coupon consists of 2 images plus some text (nothing really fancy at all).

 

Everything looks okay and as it should in the view with all the correct information etc.

 

I now what to email the coupon to the user. This is where I am a bit stumped.

 

I am assuming that I will need to save the rendered view as an image/file/.pdf if I want the styling, logos, qrcodes etc to be in the email that I will send as well as for all the formatting to be as per the view.

 

Is there any easy way or any way at all to do this?

 

In the controller I can use the response.render(view, args) but still cant see how to create a pdf file from the actual view before sending the email.

Hopefilly I ma missing something simple  

 

Thanks in advance 

 

--

Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Dave S

unread,
Jan 28, 2020, 5:22:19 AM1/28/20
to web2py-users


On Sunday, January 26, 2020 at 11:04:43 AM UTC-8, Scott Hunter wrote:
The FPDF module has an HTML Renderer which you might find useful.

- Scott

I haven't looked into that feature (that I know of), but from my previous experience with FPDF I would probably have started with doing the PDF from scratch, and then display the PDF on the page.

/dps

Reply all
Reply to author
Forward
0 new messages