# Example PDF Generator using jsPDF (Premium only)

**URL:** <https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664>\
**Category:** Show and tell\
**Tags:** tips, code\
**Created:** [October 20, 2021, 8:53am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664 "2021-10-20T08:53:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [October 20, 2021, 8:53am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/1 "2021-10-20T08:53:45Z")

</div>

Hi,  
A while ago i searched the forum to create a PDF in Wix.  
but i came up empty handed…

Now i found a way to generate a PDF using the Embed HTML frame and jsPDF.

This only works for _ **premium** _ users.

1. add this HTML/JS Code to an embed HTML frame

```javascript
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script>
<script src="https://smtpjs.com/v3/smtp.js"></script>  
<script src="https://cdn.jsdelivr.net/npm/ejs@3.1.5/ejs.min.js"></script>

  </head>
 
	
  <table id= "theBody">
    <span class="table"></span>
  </table>
    <script type="text/javascript">
	
	
	//Needed to change image to PDF format string
	function getDataUri(url)
{
    return new Promise(resolve => {
        var image = new Image();
        image.setAttribute('crossOrigin', 'anonymous'); //getting images from external domain

        image.onload = function () {
            var canvas = document.createElement('canvas');
            canvas.width = this.naturalWidth;
            canvas.height = this.naturalHeight; 

            //next three lines for white background in case png has a transparent background
            var ctx = canvas.getContext('2d');
            ctx.fillStyle = '#fff'; /// set white fill style
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            canvas.getContext('2d').drawImage(this, 0, 0);

            resolve(canvas.toDataURL('image/jpeg'));
        };

        image.src = url;
    })
}
//To have the right PDF format
var splitRegex = /\r\n|\r|\n/g;
jsPDF.API.textEx = function (text, x, y, hAlign, vAlign) {
    var fontSize = this.internal.getFontSize() / this.internal.scaleFactor;

    // As defined in jsPDF source code
    var lineHeightProportion = 1.15;

    var splittedText = null;
    var lineCount = 1;
    if (vAlign === 'middle' || vAlign === 'bottom' || hAlign === 'center' || hAlign === 'right') {
        splittedText = typeof text === 'string' ? text.split(splitRegex) : text;

        lineCount = splittedText.length || 1;
    }

    // Align the top
    y += fontSize * (2 - lineHeightProportion);

    if (vAlign === 'middle')
        y -= (lineCount / 2) * fontSize;
    else if (vAlign === 'bottom')
        y -= lineCount * fontSize;

    if (hAlign === 'center' || hAlign === 'right') {
        var alignSize = fontSize;
        if (hAlign === 'center')
            alignSize *= 0.5;

        if (lineCount > 1) {
            for (var iLine = 0; iLine < splittedText.length; iLine++) {
                this.text(splittedText[iLine], x - this.getStringUnitWidth(splittedText[iLine]) * alignSize, y);
                y += fontSize * lineHeightProportion;
            }
            return this;
        }
        x -= this.getStringUnitWidth(text) * alignSize;
    }

    this.text(text, x, y);
    return this;
};
	
      // when a message is received from the page code
      window.onmessage = (event) => {
	 console.log("event started")
       download()
      };
	  

//Sending an email
function sendEmail(pdfBase64) {
Email.send({
   SecureToken : "xxxxxxxx-xxx-xxxx-xxxx-xxxxxxxxxxxx", //Secure token made in SMTPJS.com
    To : "youremail@provider.com",//Email to send to, or multiple "xxx@xxx.xx,xxx@xxx.xx"
    From : "email used on SMTPJS",//Email the mail should be sending from
    Subject : "SUBJECT OF THE EMAIL",//Subject of the mail
    Body : `
    This is the body of the message,
Using HTML you can style it as you need.
Using `` to use HTMl over multiple lines

  `,
	Attachments : [
	{
	name : "documentname.pdf", //Name of the pdf
	data : pdfBase64	//datauristring
	}
	]
}).then(
  message => alert("Ur email has been sended.")
);
}
	    async function download() {
		
	var lineHeight = 5;
	var logo = 'https://upload.wikimedia.org/wikipedia/en/thumb/7/76/Wix.com_website_logo.svg/311px-Wix.com_website_logo.svg.png'
              var pdf = new jsPDF('p');
			  var myImage = await getDataUri(logo);
				pdf.setFontType('normal')
				pdf.setFontSize(10)
				pdf.setTextColor(7, 12, 168)
				pdf.text(10,10,"More info") //X,Y,text
				pdf.text(10,10+(lineHeight*1),"About JSPDF")
				pdf.text(10,10+(lineHeight*2),"Can be found")
				pdf.text(10,10+(lineHeight*3),"at https://mrrio.github.io/jsPDF/examples/basic.html")
				pdf.text(10,10+(lineHeight*4),"Or at the documentation website")
				pdf.text(10,10+(lineHeight*5),"https://artskydj.github.io/jsPDF/docs/jsPDF.html")
				
			  pdf.addImage(myImage,'png',10,10+(lineHeight*6),155,60) // converted image,X,Y,WIDTH,HEIGHT
			
	
			//var pdfpdfBase64 = pdf.output("datauristring")
			//sendEmail(pdfpdfBase64)
			//pdf.output("datauri")
            pdf.save("test.pdf");
			
	
	
};

    </script>
</html>

```

1. 

Add a button to your page and add the following code.

```javascript
$w("#yourhtmlelementname").postmessage("")

```

To send mails,  
Go to SMTPJS  
Scroll down to Security and press Encrypt SMTP Credentials  
Fill in the form and press generate secure token.  
Use that token in the sendMail function  
Configure it as needed and use the following lines to send the mail.

```javascript

var pdfBase64 = pdf.output("datauristring")
sendEmail(pdfBase64)

```

When using the postmessage you can add data to it.  
to run different functions in the HTML frame

```javascript
$w("#yourhtmlelementname").postmessage("sendmail") // or "download" or "opennewwindow"

```

```javascript
var todo = ""
 window.onmessage = (event) => {
	todo = event.data // event.data = postmessage data
      };

```

```javascript
if(todo === "sendmail"){

var pdfpdfBase64 = pdf.output("datauristring")
sendEmail(pdfpdfBase64) // Sends the Email
} else if(todo === "download"){
            pdf.save("test.pdf"); // saves the PDF
} else if (todo === "opennewwindow"){
pdf.output("dataurlnewwindow") // Opens the PDF in a new window
}
            

```

Output example:

 ![](https://us1.discourse-cdn.com/wix/original/3X/7/5/759a634a738694dca39e38ddde5b23d58ab83b40.png)

Hope this will help some people.

If there are any questions ,  
Feel free to ask,  
I created a table where i take the data to the htmlframe.  
And add in a new selfmade table.

 ![](https://us1.discourse-cdn.com/wix/original/3X/3/d/3d757dcac7ce1233d92b0336699a66285e39c255.png)

Kind regards,  
kristof.

---

<div class="post-metadata">

**Author:** ![miriam18346](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/miriam18346/32/8784_2.png) [@miriam18346](https://forum.wixstudio.com/u/miriam18346)\
**Post date:** [March 18, 2022, 7:33pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/2 "2022-03-18T19:33:07Z")

</div>

Hello, Kristof. Thank you for the detailed post! I’m having trouble with jspdf myself - I am just experimenting right now so have made my code very, very simple. But Wix is throwing a strange error. I was wondering if you have come across this before?

> **[Type error when using an approved npm package | Velo by Wix](https://community.wix.com/velo/forum/coding-with-velo/type-error-when-using-an-approved-npm-package)**
>
> Whilst waiting for my organisation to create a Stripe account, I am trying to create a pdf receipt upon pressing a button using the jspdf-qz package, which is an npm package approved by Wix. I'm getting a type error when I open the page in preview....

Whilst your code is much more thorough than my small experiment, the only difference of import I can see is that you included the package and dependencies in a tag, instead of downloading the npm package and importing it. Is it possible I am missing a dependency?

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [March 18, 2022, 7:41pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/3 "2022-03-18T19:41:31Z")

</div>

Ho Miriam,  
The difference is that my code is used in an embed HTML frame,  
I tried it in the page code aswel and i never whas able to make it work like that 🙂

---

<div class="post-metadata">

**Author:** ![miriam18346](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/miriam18346/32/8784_2.png) [@miriam18346](https://forum.wixstudio.com/u/miriam18346)\
**Post date:** [March 18, 2022, 7:43pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/4 "2022-03-18T19:43:07Z")

</div>

@volkaertskristof Ah! I see. Thanks very much. I wasn’t sure if the HTML was necessary - apparently so! I’ll try again.  
Thank you!

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [March 18, 2022, 7:44pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/5 "2022-03-18T19:44:16Z")

</div>

Oke, let me know if it worked or not 🙂

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [March 24, 2022, 10:32am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/6 "2022-03-24T10:32:21Z")

</div>

Hi Miriam.

I was wondring if evrything worked?

Kind regards,  
kristof.

---

<div class="post-metadata">

**Author:** ![miriam18346](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/miriam18346/32/8784_2.png) [@miriam18346](https://forum.wixstudio.com/u/miriam18346)\
**Post date:** [March 26, 2022, 4:19am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/7 "2022-03-26T04:19:00Z")

</div>

@volkaertskristof Hi Kristof. Sorry I’m just responding now - I’ve been away somewhere with no internet for a week, I left the project hanging so I’ll finish the pdf creator now. Although, I asked the organisation about them creating a Stripe account before I left. Still hasn’t happened, so I’m wondering if they’ve decided not to go ahead with the website…

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [March 26, 2022, 7:14am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/8 "2022-03-26T07:14:45Z")

</div>

Hi Miriam,  
Thats no problem 🙂  
I’m going to create a package to make a pdf easyer.  
I will let you know when its done 🙂 but sadly enough you will still need a premium account for it.

---

<div class="post-metadata">

**Author:** ![ayalayvonne30](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ayalayvonne30/32/31042_2.png) [@ayalayvonne30](https://forum.wixstudio.com/u/ayalayvonne30)\
**Post date:** [December 5, 2022, 1:31am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/9 "2022-12-05T01:31:18Z")

</div>

Could you share where you added the code to the html comp to create a table and  
how your sending the data to it. I’ve been at it for a couple days and realized after all that time that I didn’t actually need to create a html table to make the pdf from. I’ve been able to successfully create a blank pdf but I’m having issues getting my data into it.

---

<div class="post-metadata">

**Author:** ![lfanzel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/lfanzel/32/24708_2.png) [@lfanzel](https://forum.wixstudio.com/u/lfanzel)\
**Post date:** [February 7, 2023, 9:11am UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/10 "2023-02-07T09:11:37Z")

</div>

Replying to Kristof @volkaertskristof  
nice to write you

I’m trying to generate a pdf doc that takes data from a collection. I read you were intentioned to create a package about it. Is there some update?  
Thanks a lot  
Regards

---

<div class="post-metadata">

**Author:** ![user2826](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/user2826/32/94780_2.png) [@user2826](https://forum.wixstudio.com/u/user2826)\
**Post date:** [July 25, 2024, 4:21pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/11 "2024-07-25T16:21:23Z")

</div>

Hello, did you ever put a package together that lets you generate a pdf from a database on wix?

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [July 25, 2024, 8:45pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/12 "2024-07-25T20:45:29Z")

</div>

Please create your own POST, describe your requirements and what exactly is the issue you can not solve?

There are many ways of how to achieve this.

Link your own generated/opened POST to this post, if you like to.

---

<div class="post-metadata">

**Author:** ![user2826](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/user2826/32/94780_2.png) [@user2826](https://forum.wixstudio.com/u/user2826)\
**Post date:** [July 26, 2024, 2:00pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/13 "2024-07-26T14:00:24Z")

</div>

I made a new post if you could try to help it would be much appreciated.

> [@Update multiple pdf template using data from user input](https://forum.wixstudio.com/t/update-multiple-pdf-template-using-data-from-user-input/61170):
>
> There is another post here: [Generate and download a custom PDF with pdf-lib](https://forum.wixstudio.com/t/generate-and-download-a-custom-pdf-with-pdf-lib/11118) where she says she got this working but I am new at this and don’t know where or how to use the code. Or are there other examples.

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [July 26, 2024, 2:26pm UTC](https://forum.wixstudio.com/t/example-pdf-generator-using-jspdf-premium-only/5664/14 "2024-07-26T14:26:48Z")

</div>


