General
Introduction
In this section, we will cover the various actions that can be performed on PDF documents, along with the corresponding query for selecting the relevant data.
Conversion to PDF from different file formats
In AOP it is possible to convert different file formats to PDF. The possible file formats and their corresponding mime types are listed below.
File Format | Mime Type |
---|---|
application/pdf | |
docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
docm | application/vnd.ms-word.document.macroEnabled.12 |
xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
html | text/html |
md | text/markdown |
txt | text/plain |
gif | image/gif |
jpeg | image/jpeg |
jpg | image/jpeg |
png | image/png |
svg | image/svg+xml |
webp | image/webp |
bmp | image/bmp |
msbmp | image/x-ms-bmp |
doc | application/msword |
ppt | application/vnd.ms-powerpoint |
xls | application/vnd.ms-excel |
odt | application/vnd.oasis.opendocument.text |
ods | application/vnd.oasis.opendocument.spreadsheet |
odp | application/vnd.oasis.opendocument.presentation |
eml | message/rfc822 |
msg | application/vnd.ms-outlook |
csv | text/csv |
Removing Last Page From a PDF
Have you ever been in a situation where you have a PDF with a blank last page? If so, you can use the remove_last_page option to remove the last page of a PDF.
For this, in your "output" object, you can add output_remove_last_page as a boolean value. In the INIT PL/SQL section you can provide g_output_remove_last_page as true.
- SQL Option
- JSON Option
aop_api_pkg.g_output_remove_last_page:= true
{
"output": {
"output_encoding": "raw",
"output_type": "pdf",
"output_remove_last_page": true
}
}
and with any template with PDF as output type, the last page will be removed from the output PDF.
Protecting a PDF with a Password
For protecting a PDF with a password for access and/or modification, you can provide g_output_read_password and/or g_output_modify_password and/or goutput_pwd_protection_flag in the INIT PL/SQL section. It will protect the PDF from read access and/or modification. The g_output_pwd_protection_flag is optional. Click Here for more information on protection flags.
- SQL Option
- JSON Option
aop_api_pkg.g_output_read_password:= 'read_password';
aop_api_pkg.g_output_modify_password:= 'modify_password';
aop_api_pkg.g_output_pwd_protection_flag:= '4';
{
"output": {
"output_encoding": "raw",
"output_type": "pdf",
"output_read_password": "read_password",
"output_modify_password": "modify_password",
"output_password_protection_flag": "4"
}
}
and with any template with PDF as output type, the output PDF will be password protected.
You can use the output_read_password and output_modify_password options together or separately.
Adding Barcodes to PDF
AOP supports overlay of barcodes on PDF. To add barcodes to PDF Templates "output_insert_barcode" should be true. In the INIT PL/SQL section you can provide g_output_insert_barcode as true. The template type should be PDF.
In the data section, data can be provided as normal data for the AOP. The properties that are available for the barcode are:
- _type : The type of the barcode.
- _height : The height of the barcode.
- _width : The width of the barcode.
Example
Template
{|barcode}
Data Source
- SQL
- PL/SQL returning SQL
- PL/SQL returning JSON
- JSON
select 'file1' as "filename",
cursor (
select '1234567890' as "barcode",
'code128' as "barcode_type",
50 as "barcode_height",
50 as "barcode_width"
from dual
) as "data"
from dual;
declare
l_return clob;
begin
l_return := q'[
select 'file1' as "filename",
cursor (
select '1234567890' as "barcode",
'code128' as "barcode_type",
50 as "barcode_height",
50 as "barcode_width"
from dual
) as "data"
from dual;
]';
return l_return;
end;
declare
l_cursor sys_refcursor;
l_return clob; -- can also be varchar2, make sure it corresponds to the structure in the JSON
begin
apex_json.initialize_clob_output(dbms_lob.call, true, 2);
open l_cursor for
select 'file1' as "filename",
cursor (
select '1234567890' as "barcode",
'code128' as "barcode_type",
50 as "barcode_height",
50 as "barcode_width"
from dual
) as "data"
from dual;
apex_json.write(l_cursor);
l_return := apex_json.get_clob_output;
return l_return;
end;
[
{
"filename": "file1",
"data": {
"barcode":"1234567890",
"barcode_type":"code128",
"barcode_height":20,
"barcode_width":50
}
}
]
In the INIT PL/SQL section you should provide g_output_insert_barcode as true.
- SQL Option
- JSON Option
aop_api_pkg.g_output_insert_barcode:= 1; -- or true
{
"output": {
"output_insert_barcode": true,
}
}
with a template with PDF as output type, the output PDF will have the barcode.
Splitting the PDF into Multiple PDFs
For splitting a PDF into multiple PDFs, you can use the output_split option. For splitting the PDF into multiple PDFs, in INIT PL/SQL section you can provide g_output_split as true.
- SQL Option
- JSON Option
aop_api_pkg.g_output_split := 'true';
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"output_split": true
}
}
From AOP 24.3 onwards, we can now split pdf by specifying number of pages, or any string present in PDF. For that you should specify g_output_split_by_string in the INIT PL/SQL section.
- SQL Option
- JSON Option
aop_api_pkg.g_output_split := 'true';
aop_api_pkg.g_output_split_by_page := 3;
aop_api_pkg.g_output_split_by_string := 'Invoice No || Invoice Number' -- You should specify either of the parameter, if you specify both, split by page will be considered.
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"output_split": true,
"output_split_parameter": "3"
}
}
and a template with PDF as output type, the output PDF will be split into multiple PDFs. The output will be a zip file with each page of the input PDF as a separate PDF file.
Adding Watermark to PDF
Adding a watermark to a PDF can be done using the following options and parameters:
- Use the output_watermark option in the
output object
to specify the watermark text as a plain string. - To set the color of the watermark text, use output_watermark_color with a plain string. The default color value is black.
- To customize the font of the watermark text, use output_watermark_font with a plain string. The default font is Arial.
- Adjust the opacity of the watermark text using output_watermark_opacity, which takes a number representing the opacity in percentage.
- Specify the size of the watermark text using output_watermark_size with a number representing the size in pixels.
In the INIT PL/SQL
section, you have the following options:
- g_output_watermark: Set this to provide the watermark text.
- g_output_watermark_color: Use this to define the color of the watermark text.
- g_output_watermark_font: Set this to customize the font of the watermark text.
- g_output_watermark_opacity: Use this to adjust the opacity of the watermark text (in percentage).
- g_output_watermark_size: Set this to specify the size of the watermark text (in pixels).
- SQL Option
- JSON Option
aop_api_pkg.g_output_watermark:= 'watermark text';
aop_api_pkg.g_output_watermark_color:= 'red';
aop_api_pkg.g_output_watermark_font:= 'Arial';
aop_api_pkg.g_output_watermark_opacity:= 50;
aop_api_pkg.g_output_watermark_size:= 45;
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"output_watermark": "watermark text",
"output_watermark_color": "red",
"output_watermark_font": "Arial",
"output_watermark_opacity": 50,
"output_watermark_size": 45
}
}
and any template with PDF as output type, the output PDF will have the watermark text in the background. The result will look like this.
Generating PDF/A Compliant PDF
For generating PDF/A format, we can use the output_convert_to_pdfa option.
While converting using openoffice converter, specifying it will create PDF/A format, values can be either 1b
or 2b
which are the variants of PDF/A specification.
For example, if we want to convert a PDF to PDF/A-1b format, we can provide the value 1b
in the output_convert_to_pdfa option.
In INIT PL/SQL section you can provide g_output_convert_to_pdfa.
- SQL Option
- JSON Option
aop_api_pkg.g_output_convert_to_pdfa := '1b';
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"output_converter": "openoffice",
"output_convert_to_pdfa": "1b"
}
}
The PDF/A conversion is only supported by the openoffice converter. While converter handles the conversion, it is the responsibility of the template designer to ensure that the template is accessible.
and any template with PDF as output type, the output PDF will be in PDF/A-1b format.
Generating PDF/UA standard PDF
For generating PDF/UA format, we can use the output_ua_compliant_pdf option.
While converting using openoffice converter, specifying it will create PDF/UA format, values can be either true
or false
.
In INIT PL/SQL section you can provide g_output_ua_compliant_pdf.
- SQL Option
- JSON Option
aop_api_pkg.g_output_ua_compliant_pdf := 1;
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"output_converter": "openoffice",
"output_ua_compliant": true
}
}
The PDF/UA conversion is only supported by the openoffice converter. While converter handles the conversion, it is the responsibility of the template designer to ensure that the template is accessible.
Flattening (Locking) PDF Form Fields
For flattening (locking) PDF form fields, we can use the lock_form option. In the output object we can provide the value true
in the lock_form option.
In the INIT PL/SQL section you can provide g_output_lock_form as true.
- SQL Option
- JSON Option
aop_api_pkg.g_output_lock_form := true;
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"lock_form": true
}
}
Here's an example of a PDF form before and after it is flattened.
If you want to ignore signature fields while flattening, you can use the g_lock_form_ignoring_sign option. In the output object we can provide the value true
in the lock_form_except_signaturefield option.
- SQL Option
- JSON Option
aop_api_pkg.g_lock_form_ignoring_sign := true;
{
"output": {
"output_type": "pdf",
"output_encoding": "raw",
"lock_form_except_signaturefield": true
}
}
Creating Single Page PDF
Some printers can print a document which has unspecified height and tear the paper once the page is finished. These kinds of printers are used for ticketing systems. Since the height is dynamic, it is not possible to create the document in Word. AOP has a feature to create a single page PDF. This allows users to create a template in Word (or any other format) and get their output in a single page.
For that, you need to set Output Type to One Page PDF.
Duplicating the PDF into multiple copies
In AOP it is possible to duplicate the PDF into multiple copies. Lets take an example where we will be duplicating the PDF into 3 copies. For that, in the INIT PL/SQL Code section, following SQL needs to be placed.
- SQL Option
- JSON Option
aop_api_pkg.g_output_copies := 3;
{
"output": {
"output_copies": 3
}
}
Parameters for Specifying the Quality of the PDF (openoffice converter only)
If you are using openoffice converter, AOP provides options to provide the quality of the PDF. The options available are set the resolution of the image i.e. 300dpi, 600dpi, 900dpi or 1200dpi, and jpeg compression factor ranging from 0-100. The quality of the PDF can be set by using the following SQL in INIT PL/SQL Code section.
- SQL Option
- JSON Option
aop_api_pkg.g_output_image_resolution := "300dpi";
aop_api_pkg.g_output_image_compression := 100;
{
"output": {
"output_image_resolution": "300dpi",
"output_image_compression": 100
}
}
Changing the Locale used During Conversion (openoffice converter only)
AOP provides the option to change the locale used during conversion. You can use options like en , ne for the locale parameter. The locale can be changed by using the following SQL in INIT PL/SQL Code section.
- SQL Option
- JSON Option
aop_api_pkg.g_output_locale := "ne";
{
"output": {
"output_locale": "ne"
}
}
Adding Annotations/Comments To PDF
Using AOP you can add annotations to the PDF. The process of adding annotations to PDF is similar to adding texts to PDF. You can define per page annotations that you want to place on the PDF along with some other properties. The properties include x and y coordinates where you want to place the annotation on the PDF, width and height of the annotation. The default height and width of annotations are 200 units and 50 units respectively. You can also set the color of the annotation and text to be displayed in the annotation.
- SQL
- PL/SQL returning SQL
- PL/SQL returning JSON
- JSON
SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'First text to be shown in first page' AS "text",
50 AS "x",
50 AS "y",
'#FFE1BD' AS "color",
200 AS "height",
50 AS "width"
FROM
dual
UNION ALL
SELECT
'Second text to be shown in first page' AS "text",
150 AS "x",
50 AS "y",
'#00FFFF' AS "color"
FROM
dual
) AS "P1",
CURSOR (
SELECT
'First text to be shown in second page' AS "text",
50 AS "x",
50 AS "y",
'#f9f9f9' AS "color"
FROM
dual
) AS "P2",
CURSOR (
SELECT
'Page Commenting here' AS "text",
0 AS "x",
0 AS "y",
'#000000' AS "color"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_comments"
FROM
dual
) AS "data"
FROM
dual
declare
l_return clob;
begin
l_return := q'[
SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'First text to be shown in first page' AS "text",
50 AS "x",
50 AS "y",
'#FFE1BD' AS "color",
200 AS "height",
50 AS "width"
FROM
dual
UNION ALL
SELECT
'Second text to be shown in first page' AS "text",
150 AS "x",
50 AS "y",
'#00FFFF' AS "color"
FROM
dual
) AS "P1",
CURSOR (
SELECT
'First text to be shown in second page' AS "text",
50 AS "x",
50 AS "y",
'#f9f9f9' AS "color"
FROM
dual
) AS "P2",
CURSOR (
SELECT
'Page Commenting here' AS "text",
0 AS "x",
0 AS "y",
'#000000' AS "color"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_comments"
FROM
dual
) AS "data"
FROM
dual
]';
return l_return;
end;
declare
l_cursor sys_refcursor;
l_return clob; -- can also be varchar2, make sure it corresponds to the structure in the JSON
begin
apex_json.initialize_clob_output(dbms_lob.call, true, 2);
open l_cursor for
SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'First text to be shown in first page' AS "text",
50 AS "x",
50 AS "y",
'#FFE1BD' AS "color",
200 AS "height",
50 AS "width"
FROM
dual
UNION ALL
SELECT
'Second text to be shown in first page' AS "text",
150 AS "x",
50 AS "y",
'#00FFFF' AS "color"
FROM
dual
) AS "P1",
CURSOR (
SELECT
'First text to be shown in second page' AS "text",
50 AS "x",
50 AS "y",
'#f9f9f9' AS "color"
FROM
dual
) AS "P2",
CURSOR (
SELECT
'Page Commenting here' AS "text",
0 AS "x",
0 AS "y",
'#000000' AS "color"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_comments"
FROM
dual
) AS "data"
FROM
dual
apex_json.write(l_cursor);
l_return := apex_json.get_clob_output;
return l_return;
end;
[
{
"filename": "file1",
"data": [
{
"aop_pdf_comments": [
{
"P1": [
{
"text": "First text to be shown in first page",
"x": 50,
"y": 50,
"color": "#FFE1BD",
"height": 200,
"width": 50
},
{
"text": "Second text to be shown in first page",
"x": 150,
"y": 50,
"color": "#00FFFF"
}
],
"P2": [
{
"text": "First text to be shown in second page",
"x": 50,
"y": 50,
"color": "#f9f9f9"
}
],
"all": [
{
"text": "Page Commenting here",
"x": 0,
"y": 0,
"color": "#000000"
}
]
}
]
}
]
}
]
With any Template whose output is PDF, we can add annotations to the PDF.
Validating a PDF document
Available from: v24.3In AOP for a PDF template, you can validate if the document is a valid PDF document. For this, you need to set output_type parameter to validate_pdf. The response will be a JSON object with the following keys:
- valid: A boolean value indicating if the document is a valid PDF document.
- message: A message indicating the result of the validation.
There might be three possible values for the message key:
- When a document is a valid PDF document (valid: true), the message will be PDF is valid.
- When a document is not a valid PDF document (valid: false), the message will start with Error: followed by the exception obtained.
- When the document is password protected (valid: true) the message will be PDF is password protected.
Example
Here is an example of how a PDF can be validated.
PLSQL Code
PL/SQL code used to call AOP, which returns a JSON response in a CLOB.
declare
l_output_filename varchar2(300);
l_blob blob;
l_clob clob;
begin
l_blob := aop_api_pkg.plsql_call_to_aop(
p_data_type => aop_api_pkg.c_source_type_none,
p_template_type => aop_api_pkg.c_source_type_apex,
p_template_source => 'aop_pdf_form_filled.pdf', -- Your template source
p_output_type => aop_api_pkg.c_validate_pdf, -- output type as "validate_pdf"
p_output_filename => l_output_filename,
p_aop_url => 'https://api.apexofficeprint.com/',
p_api_key => :AOP_API_KEY, --your api key
p_app_id => :APP_ID
);
l_clob := aop_api_pkg.blob2clob(l_blob);
l_clob := replace(l_clob,'],','],'||CHR(10));
return l_clob;
end;
Template
For the template, you can use any PDF file.
Output
When the given template is valid, as shown in the image above, AOP will provide the JSON response {"valid":true,"message":"PDF is valid"}
. However, the response for others will be as listed below.
//for valid PDF
{
"valid": true,
"message": "PDF is valid"
}
//for password protected PDF
{
"valid": true,
"message": "PDF is password protected"
}
//for invalid PDF
{
"valid": false,
"message": "Error: Header doesn't contain versioninfo"
}
Converting PDF to Images
Available from: v24.3AOP allows you to convert a PDF document into images. For this you just need to set the Output Type
as image(jpeg), and a ZIP file containing all the images will be returned as the output.
Currently, conversion from PDF to JPEG is the only supported format.
Example
Here is an example of how a PDF can be converted into images using AOP.
Data Source
The dynamic action settings for converting a PDF into images is shown below.
You need to select Image(jpeg) as the Output Type in the dynamic action.
Template
For the template, you can use any PDF file.
Output
Upon processing the provided template and setting Output Type
as Image(jpeg), the resulting output is as follows.