PDF GENERATOR API npm demo

Now with the help of npm we can easily create a PDF using pdf generator api

A PDF is created and downloaded when the user submited a form. Usage: Create a Invoice, Create a certificate for the user etc. Sample Website link

Requirement:

  1. Free account in pdfgeneratorapi

Overall Layout:

i) PDF GENERATOR API
ii) Wix code Backend (npm - pdf-generator-api)
iii)Page Code
iv) HTML Embed code (Convert Base64 into PDF file)

i) PDF GENERATOR API

  1. Goto pdfgeneratorapi 2. Sign In and goto Dashboard 3. Select the “Template” on the left side bar 4. Click on the “New Template” button, A new window open to edit the pdf File 5. Design the PDF and to create a variable use open and close Curly brackets as shown on this image


[This is a image of the pdf generator Editor {Name} where the variable will be later assign using wix code And there is one more variable, {Detail} to add the message] 6. Publish the template and Goto the “Templates” from the Dashboard 7. Copy the Template Id from the “Templates”


8. Copy the APIKey and APISecret from the “Account Settings”


9. Copy the WorkSpace Identifier from the “Admin Panel”

ii) Wix code Backend

  1. Search and Install “pdf-generator-api” npm (Check this link for Installation guide and Node package manager npm ) 2. Create a Backend Web Module “pdf.jsw”. 3. Copy and paste the code

// Code start
import PDFGeneratorAPI from ‘pdf-generator-api’;

export async function pdf(name, detail) {
let Client = new PDFGeneratorAPI(
‘’,
‘’
);
Client.setWorkspace(‘’);
let obj = {
“Name” : name,
“Detail” : detail,
}
let response = await Client.output(‘’, obj)
return response.response
}
// Code end

  1. Replace the , , and to your corresponding value grab from the pdfgeneratorapi site 5. Create a object for the variable in this case i have create an object " obj " with two Key “Name” and “Detail” and the corresponding value directly from the parameter of this async function “name” and “detail”

iii)Page Code

  1. Drag and drop Input element, Text box element and Button in a new page 2. Also Drag and drop the “HTML embed” element from “more” section 3. Change the Property name as shown in the image


4. Copy and paste the below code

// Code start
import { pdf } from ‘backend/pdf.jsw’;

$w.onReady(function () {
var base64;
$w(’ #btnSubmit ‘).onClick(() => {
let name = $w(’ #inName ‘).value;
let detail = $w(’ #inDetail ‘).value;
pdf(name, detail).then(e => {
base64 = e;
base64 = ‘data:application/pdf;base64,’ + base64
let msg = {
“conv”: true,
“dataUrl”: base64
}
$w(’ #html1 ').postMessage(msg);
});
});
});
// Code end

iv) HTML Embed code

  1. Click on the HTML embed element 2. Click on “Edit code” button 3. Paste the following code [updated 02-dec-2018]

Overall codes


Congrats ! That’s it

Original post : https://www.salman2301.com/forum/wix-code-advance/create-pdf-using-wix-code-and-pdf-generator-api

Let me know if you have any questions

6 Likes

Great tutorial man!

Very helpful. Just 1 question: why was there the need to use the html-component instead of calling all from the backend?

Interesting though by Giri. Would be much better if we could call it and get like the url and send in an email to clients?

You can:

let outputtype = “url”
let pdf = await Client.output(‘39011’, obj ,“pdf”,“directory”,{ “output”: outputtype })
return pdf.response

The NodeJS documentation does not show all the parameters that you can include on the output function. I looked at the source code and found that you can send the output function the template number (39011), the data to merge (obj), the document type to generate (pdf), a document name (directory), and the output type (outputtype = url). This will generate a valid url that can be used in wixLocation.to().

Also, because the PDF Generator API accepts JSON data, you can create reports from your collection data.

Thanks guys
Thank you @davebogan98 !! lol next time i should check the doc

Hi guys, I have applied this method and all it is working perfectly, however I have a trouble: I am using the output type (url) and I want to save the generated pdf directly into a database item (document) in the same moment in which the user is downloading the file through the url generated by the api. I have alredy tried to use the url as it is and save it into the database item throug wix-data however this attempt does not work. I have also created a free-access folder on google drive with a public address can I send the pdf directly to the folder?
Thx

It is not clear from the PDF Generator API website how long the generated pdf exists in their domain, or whether you can download it as a file — I have not tried this. The Wix Code file upload api is based on the special file upload button (as best I can tell), which assumes an upload from the local file system of the user. It might be possible to generate the pdf as a base64 encoded string, store that in a collection, and then use some of Salman’s code example to display it through an html component. Have not tried this, either.

@davebogan98 if I choose the option to get the base64 encoded file, can I directly link the output to the value parameter of the wix upload button?

@congiumat The upload button uploads files to the Media Manager, not to a collection, so it is limited to the supported document types. You would use the wixdata api to store the base64 string in a database collection.

Thank you I will try to link the base64 string to the db item.

@davebogan98 i have tried to link the base64 to a database item throug wix-data.insert() however the web app Is crushing due to the too high payload XD

@congiumat Well… you have about reached the limit of my abilities. Perhaps someone else on the forum will have a suggestion.

@congiumat it saved for me, did you try to saving it from backend.

@salman-hammed hi salman I have tried to save it from the client side, can you post the backend code here? Are you saving it as an object (item) with wix-data ? Thank you for the reply

@congiumat
Just the simple code
‘title’ , ‘detail’ and ‘pdfBase64’ are the field Keys of ‘pdf’ database

import PDFGeneratorAPI from 'pdf-generator-api';
import wixData from 'wix-data';

export async function pdf(name, detail) {
 let Client = new PDFGeneratorAPI(
 '<apikey>',
 '<apisecrect>',
    );
    Client.setWorkspace('<workspace>');
 let obj = {
 "Name" : name,
 "Detail" : detail,
    }
 let response = await Client.output(<template Id>, obj);
 let item = {
 "title" : name,
 "detail" : detail,
 "pdfBase64" : response.response
    }
    wixData.insert('pdf' , item)
 return response.response
}

@salman-hammed It is working perfectly, thx now i can store the base64 data and retrieve it from the database using the html script. Thank you very much for your help!

@congiumat Happy to help!!

Hi @ben ,
To accept Hebrew language
Select the text and on the format section
Select the Type dropdown to “SYSTEM”

PDF text link
https://salman2301.wixsite.com/pdfnpm
PDF link with Hebrew language
https://pdfgeneratorapi.com/share/3729/21d3ab779536fd7b73bbf1bbae8b953c

@ben Awesome! Keep coding