Develop the Web Application
Developing a web application requires completing these tasks:
- Create a Visual Builder Application.
- Create a New web application.
- Include the external library.
- Create a user interface.
- Update the uploaded PDF document.
- Create a new PDF document.
- Download the PDF file.
Create a Visual Builder Application
A Visual Builder application is a collection of resources that you use to develop web and mobile applications. A Visual Builder application includes metadata in JSON files that describe data sources and business objects. It also includes HTML and JavaScript files for the web and mobile applications.
- :On the Visual Applications home page, click New.
- In the Create Application dialog box, type the application name.
The Application ID field is automatically populated based
on the application name that you provide.
- You can modify the application ID if necessary, but the ID must be unique in your identity domain.
- Leave the default selection of Empty Application unchanged. This template does not create any artifacts, apps or other resources.
- Click Finish.
A new Visual Builder application opens automatically after you create it and displays the Welcome screen. The new application will not contain any artifacts, but the wizard does create, by default, the application's file structure and some resources and files you will need.
Create a New Web Application
The web apps in your Visual Builder application are independent of each other, but they can all use the data sources defined in the application.
Include External Libraries
To generate, modify, and download a PDF document, as specified in “Before you
Begin", you need to include two external libraries: pdf-lib
and
downloadj
.
pdf-lib
, manages the PDF document.downloadj
, downloads the PDF document that has been generated.
index.html
file:
<script src="https://unpkg.com/pdf-lib@1.4.0"></script>
<script src="https://unpkg.com/downloadjs@1.4.7"></script>
Create a User Interface
Now, you create a user interface. This interface should contain such components as containers, headers, buttons, text fields, JavaScript code, and HTML code and will allow users to:
- Upload a file.
- Enter into a field text that will appear in the PDF document.
- Click a button to update the uploaded PDF document with the text they entered.
- Click a button to create a new PDF document that contains the text they entered.
- Click a button to download the prepared document.
Drag and Drop the File and Display the PDF Document
First, you need to drag and drop essential components into the UI you're building.
Display the Uploaded PDF File to the Main Page Screen
In the main page's source HTML, add an object with its source the previously
assigned page variable fileURL
. Add the fileURL
object to the main HTML page. At this stage, the application can upload and display
a PDF file.
Assign the Output of the addPDFDocument Function to the Page Variables fileURL and holdFile
Assign the returned addPDFDocument
function properties URL
and data to the page variables fileURL
and
holdFile
, respectively.
Assign the Input Variable of the addPDFDocument Function
On the GUI, assign the uploaded file variable to the function's
arg1
parameter.
Create addPDFDocument function
addPDFDocument
at the page
level. The application will receive one input parameter. The function will instantiate a URL
variable based on the input file parameter and then will read the uploaded document and
convert it to a base64 string. You can use this code sample to define the
function:
PageModule.prototype.addPDFDocument = function(arg1) {
return new Promise(
resolve=>{
const blobURL = URL.createObjectURL(arg1);
const reader = new FileReader();
reader.addEventListener("load", function () {
// convert image file to base64 string
resolve({ data: reader.result, url: blobURL });
}, false);
if (arg1) {
reader.readAsDataURL(arg1);
}
}
);
};
Update Uploaded PDF Document
From the components palette drag and drop into the page designer a Text Field component and, below this, a new button.
- Create a page variable
textIncludedIntoPDF
to store the content of the text field. This variable will insert the content and update the PDF document with it. - Assign this variable to the Input Text component.
Update and Display PDF
update_display_pdf
.
Create the UpdatePDF Function
Next, create the JavaScript function createUpdatePDF
. This
is an asynchronous function with two input parameters (the uploaded PDF document and the
text that the user has previously entered on the main page). It returns a byte array.
Display the Updated PDF Document
The outcome of UpdatePDF
function will be assigned to the
temporary page variable tmpPDFBytes
. Following this assignment, a
JavaScript function will receive the base64 byte array and it will instantiate a new blob
entity from this byte array. Finally, the function will create and return a URL that
correspond to this PDF document.
PageModule.prototype.displayPDFDocument = function (arg1) {
const bytes = new Uint8Array( arg1 );
// create a new blobfrom the byte array with type of application: pdf
const blob = new Blob( [ bytes ], { type: "application/pdf" } );
// create the url of the generated blob entity
const newdocURL = URL.createObjectURL(blob);
// return the updated pdf's url
return newdocURL;
};
This
function's output will be assigned to the page variable fileURL
, which
will be used to display the updated PDF document in the page.
At this point the application is ready to upload a PDF document, modify it with the text the user has entered in the screen's text field and then display the updated PDF document on the screen.
Create a New PDF Document
Create and Display Document
Create a new event to define the action launched by the Create new
PDF button. The event will create a new action chain with the label
create_display_pdf
.
The new action chain will use a JavaScript function to create a new PDF (which will include text previously entered in the text field) and then, through a temporary page variable, a second JavaScript function will regenerate and display the new PDF document.
Create the PDF Function
Next, create the JavaScript function createPDF
. This is an
asynchronous function that takes text that the user has previously entered on the main page
as its one input parameter and returns a byte array.
Display the Newly Created PDF Document
createPDF
function will be
assigned to the temporary page variable tmpPDFBytes
. Following this
assignment, a JavaScript function will receive the base64 byte array and will
instantiate a new blob entity from this byte array. .
The function will then create and return a URL that corresponds to this PDF
document. The same function that has been implemented for the
update_display_pdf
action chain will be reused. The output of this
function is then assigned to the page variable fileURL
, which will be
used to display the new PDF document in the page
Download the PDF File
From the components palette we include into the page designer by drag and dropping a Button component.
Download the Document
Create a new event action for the Download PDF button.
The event will create a new action chain, labeled
downloadPDF
. The new action chain
will use a JavaScript function to download a PDF file (containing
the text the user entered text in the text field).
Download the PDF File
downloadPDFfile
takes the
byte array of the PDF document that was either uploaded and modified or created from
scratch as its one input parameter and downloads the document with a predefined name:
pdf-lib_modification_example.pdf
and of type
application/pdf
.
download()
function (from the library
downloadj
that was initially hidden and then imported into your
application). The input arguments for download()
can be any of these
data types::
- URL
- String
- Blob
- Typed Array
dataURL
representing the file's data as a base64 or
URL-encoded string, the name and type of the new file placed in the browser's download
directory.
PageModule.prototype.downloadPDFFile = function (arg1) {
download(arg1, "pdf-lib_modification_example.pdf", "application/pdf");
};