Friday, March 22, 2024

To generate a PDF using JavaScript in Oracle APEX from a collection

 To generate a PDF using JavaScript in Oracle APEX from a collection, you can follow these steps:


1. Create a button or link on your APEX page to trigger the PDF generation.
2. Add a dynamic action to the button or link to execute JavaScript code.
3. In the JavaScript code, retrieve the data from the collection and format it as needed.
4. Use a JavaScript library like jsPDF or pdfmake to generate the PDF document.
5. Download the generated PDF document.

Here's an example of how you can achieve this:

1. Create a button or link on your APEX page:
  - Button Name: "Generate PDF"
  - Button Action: "Defined by Dynamic Action"

2. Add a dynamic action to the button or link:
  - Event: "Click"
  - Action: "Execute JavaScript Code"

3. In the JavaScript code, retrieve the data from the collection and format it:
```javascript
// Retrieve data from the collection
var collectionData = apex.collection.get('YOUR_COLLECTION_NAME');

// Format the data as needed
var formattedData = '';
for (var i = 0; i < collectionData.length; i++) {
 formattedData += collectionData[i].column1 + ' - ' + collectionData[i].column2 + '\n';
}
```

4. Use a JavaScript library like jsPDF or pdfmake to generate the PDF document:
  - Include the library in your APEX application (e.g., by uploading the library file to the Static Application Files).
  - Use the library's API to create and format the PDF document based on the formatted data.

Here's an example using jsPDF:
```javascript
// Generate the PDF document
var doc = new jsPDF();
doc.text(formattedData, 10, 10);

// Save the PDF document
doc.save('generated_pdf.pdf');
```

5. Download the generated PDF document:
  - The `doc.save('generated_pdf.pdf')` line in the above code will automatically download the PDF document with the specified filename.

Make sure to replace `'YOUR_COLLECTION_NAME'` with the actual name of your collection, and adjust the formatting and PDF generation code as per your requirements.

Note: Ensure that you have the necessary permissions and libraries set up in your Oracle APEX environment to execute JavaScript code and generate PDF documents.

Sunday, March 10, 2024

How to call a REST API with PL/SQL

Answer:

Here are the general steps on how to call a REST API using PL/SQL:

1. Register the REST API: First thing you must do is to let Oracle know that a REST service exists and is available. You can define your REST service with Oracle using the APEX_WEB_SERVICE package.

2. Create a HTTP request to call the REST API: Once the REST API is registered in Oracle, you can use the UTL_HTTP package to create a HTTP request to call the REST API.

Here's a simple example of what code might look like:

DECLARE
l_url VARCHAR2(500);
l_response CLOB;
l_http_request UTL_HTTP.req;
l_http_response UTL_HTTP.resp;
BEGIN
--Define API url
l_url := 'http://<api_url>';

-- Make a HTTP request to the REST API
l_http_request := UTL_HTTP.begin_request(l_url, 'POST', 'HTTP/1.1');

-- Set Header parameters
UTL_HTTP.set_header(l_http_request, 'Content-Type', 'application/x-www-form-urlencoded');

-- Send Request and get Response
l_http_response := UTL_HTTP.get_response(l_http_request);

-- Get the JSON response
UTL_HTTP.read_text(l_http_response, l_response);

UTL_HTTP.end_response(l_http_response);
EXCEPTION
WHEN utl_http.end_of_body THEN
dbms_output.put_line('Response:'||l_response);
WHEN OTHERS THEN
dbms_output.put_line('Error in http request:'||SQLERRM);
END;
/


Disclaimer: You need to replace '<api_url>' with your actual REST API URL.
Also, error handling and security considerations like SSL certificate, handling sensitive information etc., should be properly implemented.

Keep in mind that network ACLs have to be properly configured so PL/SQL can reach network resources. Also, don't forget to handle any potential exceptions.

Please note, in Oracle 19c and above you can also use the DBMS_CLOUD package to send HTTP requests, which can simplify the code significantly if you are working in the Oracle Cloud.

To generate a PDF using JavaScript in Oracle APEX from a collection

  To generate a PDF using JavaScript in Oracle APEX from a collection, you can follow these steps: 1. Create a button or link on your APEX p...