Process custom PDF files.
Introduction
Netuno generates PDF files with support for images, tables, fonts, and much more, through the PDF feature, which provides a low-code abstraction of iTextPDF for the various programming languages supported by Netuno.
The application that comes with Netuno called demo (demonstration application) contains many examples of possible code implementations in various languages, including the demonstration of PDF export through the export-pdf service located at:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
netuno/apps/demo/server/services/samples/javascript/export-pdf.js
netuno/apps/demo/server/services/samples/typescript/export-pdf.ts
netuno/apps/demo/server/services/samples/python/export-pdf.py
netuno/apps/demo/server/services/samples/ruby/export-pdf.rb
netuno/apps/demo/server/services/samples/kotlin/export-pdf.kts
netuno/apps/demo/server/services/samples/groovy/export-pdf.groovy
Initialization
First, you need to create the object that will contain the context of the new document being created; by default, the page size is A4.
When creating the document, you define where it will be generated.
If nothing is defined, it will be generated in the service's data output, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument()
const pdfDocument = _pdf.newDocument()
pdfDocument = _pdf.newDocument()
pdfDocument = _pdf.newDocument()
val pdfDocument = _pdf.newDocument()
def pdfDocument = _pdf.newDocument()
To generate a PDF file inside the application's docs folder, you can do it like this:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
const pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
val pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
def pdfDocument = _pdf.newDocument(_app.file("docs/file.pdf").output())
If you want to generate the document in a new file outside the application, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
const pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
val pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
def pdfDocument = _pdf.newDocument(_os.file("/tmp/file.pdf").output())
Or even in the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
const pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
val pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
def pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output())
By default, the document is created with the A4 page format.
Page Format
To customize the page format, simply specify it when creating the document.
Creates a new document for the service's data output with pages in letter format:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
const pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
val pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
def pdfDocument = _pdf.newDocument(_pdf.pageSize("letter"))
Creates a new document for a file at the root of the application with B3 page format:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
const pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
val pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
def pdfDocument = _pdf.newDocument(_app.file("file.pdf").output(), _pdf.pageSize("B3"))
Creates a new document in the application's storage with legal page format:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
const pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
val pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
def pdfDocument = _pdf.newDocument(_storage.filesystem("server", "file.pdf").output(), _pdf.pageSize("legal"))
See the documentation about page size definitions.
PDF documents can be created with a custom page size definition; to do this, use pageSize by passing the page width and height values, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_pdf.pageSize(50, 30)
_pdf.pageSize(50, 30)
_pdf.pageSize(50, 30)
_pdf.pageSize(50, 30)
_pdf.pageSize(50, 30)
_pdf.pageSize(50, 30)
HTTP Header
To create a service in the REST API that returns a PDF file as data output, you need to add the HTTP protocol's Content-Type parameter to the header.
Example of how to set the content as PDF:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_header.contentType("pdf")
_header.contentType("pdf")
_header.contentType("pdf")
_header.contentType("pdf")
_header.contentType("pdf")
_header.contentType("pdf")
The pdf code is shorthand for:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_header.contentType("application/pdf")
_header.contentType("application/pdf")
_header.contentType("application/pdf")
_header.contentType("application/pdf")
_header.contentType("application/pdf")
_header.contentType("application/pdf")
Page Break
To create a new page, areaBreak is used:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
_pdf.areaBreak()
)
pdfDocument.add(
_pdf.areaBreak()
)
pdfDocument.add(
_pdf.areaBreak()
)
pdfDocument.add(
_pdf.areaBreak()
)
pdfDocument.add(
_pdf.areaBreak()
)
pdfDocument.add(
_pdf.areaBreak()
)
Paragraph
Adds a new paragraph to the document:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("My text here...")
.setFontSize(15)
)
To add a paragraph at a fixed position:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
pdfDocument.add(
pdfDocument.add(
_pdf.paragraph("My fixed text...")
.setFixedPosition(100, 500, 200)
.setFontSize(15)
)
)
In the code above, in the setFixedPosition method, the first parameter is the X position (100), the second parameter is the Y position (500), and the third parameter is the width (200).
Attention: if Y is set to the value 0, it means it stays at the bottom of the page, so to move it up you need to increase the Y value; the higher the Y value, the closer to the top it will appear, but if it is too high beyond the page length, it will not be visible.
Documentation with all methods of the iText Paragraph object.
Font
To define a native font, we can do it as follows:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const helvetica = _pdf.font("helvetica")
const helveticaBold = _pdf.font("helvetica-bold")
const helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
const helveticaOblique = _pdf.font("helvetica-oblique")
const helvetica = _pdf.font("helvetica")
const helveticaBold = _pdf.font("helvetica-bold")
const helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
const helveticaOblique = _pdf.font("helvetica-oblique")
helvetica = _pdf.font("helvetica")
helveticaBold = _pdf.font("helvetica-bold")
helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
helveticaOblique = _pdf.font("helvetica-oblique")
helvetica = _pdf.font("helvetica")
helveticaBold = _pdf.font("helvetica-bold")
helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
helveticaOblique = _pdf.font("helvetica-oblique")
val helvetica = _pdf.font("helvetica")
val helveticaBold = _pdf.font("helvetica-bold")
val helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
val helveticaOblique = _pdf.font("helvetica-oblique")
def helvetica = _pdf.font("helvetica")
def helveticaBold = _pdf.font("helvetica-bold")
def helveticaBoldOblique = _pdf.font("helvetica-boldoblique")
def helveticaOblique = _pdf.font("helvetica-oblique")
To define a custom made font, we can use storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
const viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
val viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
def viksiScript = _pdf.font(_storage.filesystem("server", "samples/export-pdf", "viksi-script.ttf"), true)
We can also use a file inside the application, as this example shows, where the font is inside the fonts folder at the root of the application:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
const roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
val roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
def roboto = _pdf.font(_app.file("fonts/roboto.ttf"), true)
Or even use a file at any path on the system, so in this example the font is inside the fonts folder, which is inside the system's temporary files folder:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
const roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
val roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
def roboto = _pdf.font(_os.file("/tmp/fonts/roboto.ttf"), true)
Documentation with all methods of the iText Font object.
To use the font in the texts, you must pass the font constant to the paragraph, like this:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
pdfDocument.add(
_pdf.paragraph("Using the bold font.")
.setFont(helveticaBold)
.setFontSize(15)
)
Image
Inserting images into a PDF document generated programmatically is done like this:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
pdfDocument.add(
_pdf.image(_storage.filesystem("server", "samples/export-pdf", "logo.png"))
.scaleAbsolute(120, 36)
)
Note that the scaleAbsolute method is very useful for resizing and adjusting the image size.
Alternatively, we can use a file inside the application, as in this example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const imageLogo = _pdf.image(_app.file("public/images/logo.png")
const imageLogo = _pdf.image(_app.file("public/images/logo.png")
imageLogo = _pdf.image(_app.file("public/images/logo.png")
imageLogo = _pdf.image(_app.file("public/images/logo.png")
val imageLogo = _pdf.image(_app.file("public/images/logo.png")
def imageLogo = _pdf.image(_app.file("public/images/logo.png")
Or even use a file at any path on the system, as in this example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
const imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
val imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
def imageLogo = _pdf.image(_os.file("/tmp/images/pdf-logo.png")
To add an image at a fixed position:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
pdfDocument.add(
imageLogo
.scaleAbsolute(120, 36)
.setFixedPosition(100, 500)
)
In the above, in the setFixedPosition method, the first parameter is the X position (100), and the second parameter is the Y position (500).
Attention: if Y is set to the value 0, it means it stays at the bottom of the page, so to move it up you need to increase the Y value; the higher the Y value, the closer to the top it will appear, but if it is too high beyond the page length, it will not be visible.
Documentation with all methods of the iText Image object.
Table
It is possible to create tables, which allows presenting a data set in a more readable way.
The number of table columns is defined with the parameter: _pdf.table(numberColumns)
In the example below, numberColumns is the number 1, that is, one column.
Since we add two cells to the table, the second cell goes to the next row.
Note:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
pdfDocument.add(
_pdf.table(1)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Person")
.setFont(helvetica)
.setFontSize(10)
)
.setBorder(_pdf.border("solid", 2))
.setBackgroundColor(_pdf.colorRGB(1, 0, 0))
)
.addCell(
_pdf.cell()
.add(
_pdf.paragraph("Nicole")
.setFont(helvetica)
.setFontSize(10)
)
.setBackgroundColor(_pdf.colorGray(0.5))
)
)
The addCell method allows inserting the cell definitions with their respective content.
Documentation with all methods of the iText Cell object.
Closing and Saving the Document
The document is only fully saved or generated when the close method is called, as follows:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
pdfDocument.close()
pdfDocument.close()
pdfDocument.close()
pdfDocument.close()
pdfDocument.close()
pdfDocument.close()
Where the document is generated is defined at initialization, so it is when initializing the document that we define where the final document will be generated, whether in the service's data output (output) or in a file.
Editing a File
It is possible to edit an existing PDF document, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
const pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
val pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
def pdfDocument = _pdf.openDocument(_app.file("file.pdf"))
The generated final document will be sent to the service's data output.
It is very useful for using a PDF as a base, as if it were a template.
Example of editing a document in the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
const pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
val pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
def pdfDocument = _pdf.openDocument(_storage.filesystem('server', 'file.pdf'))
Uses a document as a base but saves the changes to a new PDF file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
const pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
val pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
def pdfDocument = _pdf.openDocument(
_app.file("base-document.pdf"),
_app.file("final-document.pdf")
)
Example using a document in storage as a base but saving it to another one:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
const pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
val pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
def pdfDocument = _pdf.openDocument(
_storage.filesystem("server", "base-contract.pdf"),
_storage.filesystem("server", "final-contract.pdf")
)
With the document loaded, we can add content, and when closing it with the close method at the end, the final document is then generated with the new content added programmatically.
Example of a service that uses an existing PDF as a template to generate a PDF file output with dynamically added content:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_header.contentType("pdf")
const pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
_header.contentType("pdf")
const pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
_header.contentType("pdf")
pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
_header.contentType("pdf")
pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
_header.contentType("pdf")
val pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
_header.contentType("pdf")
def pdfDocument = _pdf.openDocument(_app.file("template-1.pdf"))
pdfDocument.add(
_pdf.paragraph("My dynamic content.")
.setFixedPosition(37, 730, 100)
.setFontSize(15)
)
pdfDocument.close()
Reading a File
To read a PDF document, extracting the text information.
We use the extract method, which gets all the PDF file data into a JSON object.
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
const pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
val pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
def pdfData = _pdf.extract(_app.file("file-1.pdf"))
_out.json(pdfData)
The returned JSON has the content key with all the text content of the PDF file, and has the metadata key with the various properties of the PDF file.
Here is an example of the JSON returned by extract:
{
"content": "All the text content of the PDF file goes here...",
"metadata":{
"Content-Type":"application/pdf",
"dc:format":"application/pdf; version=1.4",
"dc:title":"Linux 1",
"pdf:PDFVersion":"1.4",
"pdf:charsPerPage":"580",
"pdf:containsDamagedFont":"false",
"pdf:containsNonEmbeddedFont":"false",
"pdf:docinfo:producer":"Skia/PDF m125 Google Docs Renderer",
"pdf:docinfo:title":"File 1",
"pdf:encrypted":"false",
"xmpTPg:NPages":"3",
...
}
}
There are many other data items obtained in metadata but omitted in ... so the example does not become too long.
Converting
It is possible to programmatically convert the PDF to text or even to HTML.
PDF to Text
We can extract all the PDF file content as plain text using the method:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_pdf.toText(...)
_pdf.toText(...)
_pdf.toText(...)
_pdf.toText(...)
_pdf.toText(...)
_pdf.toText(...)
Example of how to use it to read a file at the root of the application:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_out.print(_pdf.toText(_app.file("my-document.pdf")))
_out.print(_pdf.toText(_app.file("my-document.pdf")))
_out.print(_pdf.toText(_app.file("my-document.pdf")))
_out.print(_pdf.toText(_app.file("my-document.pdf")))
_out.print(_pdf.toText(_app.file("my-document.pdf")))
_out.print(_pdf.toText(_app.file("my-document.pdf")))
Or even using a file in the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toText(_storage.filesystem("server", "my-document.pdf")))
PDF to HTML
We can extract all the PDF file content as HTML using the method:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_pdf.toHTML(...)
_pdf.toHTML(...)
_pdf.toHTML(...)
_pdf.toHTML(...)
_pdf.toHTML(...)
_pdf.toHTML(...)
Example of how to use it to read a file at the root of the application:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
_out.print(_pdf.toHTML(_app.file("my-document.pdf")))
Or even using a file in the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
_out.print(_pdf.toHTML(_storage.filesystem("server", "my-document.pdf")))
HTML to PDF
It is possible to generate PDF files from HTML code using the method:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_pdf.fromHTML(...)
_pdf.fromHTML(...)
_pdf.fromHTML(...)
_pdf.fromHTML(...)
_pdf.fromHTML(...)
_pdf.fromHTML(...)
Example of how to use it to generate a downloadable file in the service:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const icon = _app.file("public/images/icon.png");
const pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto;
}
div.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
width: 100px;
background: #1B5886;
}
div.sidebar > div {
position: absolute;
top: 350px;
transform: rotate(-90deg) translate(-50%, -34px);
font-family: Limelight;
font-size: 40px;
color: #89CFF1;
}
main {
padding-left: 150px;
}
h1 {
margin: 0 auto;
text-align: center;
font-family: Limelight;
color: #BA250E;
}
table {
margin: 0 auto;
border: 1px solid black;
border-spacing: 0;
border-collapse: collapse;
}
table > thead {
background: #40712A;
color: #fff;
}
table td, table th {
padding: 5px;
text-align: center;
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1;
}
a {
font-family: Limelight;
font-size: 16px;
font-weight: bold;
text-decoration: none;
color: #1E5FB5;
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`);
_header.contentTypePDF();
_out.copy(pdfFile.input());
const icon = _app.file("public/images/icon.png");
const pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto;
}
div.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
width: 100px;
background: #1B5886;
}
div.sidebar > div {
position: absolute;
top: 350px;
transform: rotate(-90deg) translate(-50%, -34px);
font-family: Limelight;
font-size: 40px;
color: #89CFF1;
}
main {
padding-left: 150px;
}
h1 {
margin: 0 auto;
text-align: center;
font-family: Limelight;
color: #BA250E;
}
table {
margin: 0 auto;
border: 1px solid black;
border-spacing: 0;
border-collapse: collapse;
}
table > thead {
background: #40712A;
color: #fff;
}
table td, table th {
padding: 5px;
text-align: center;
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1;
}
a {
font-family: Limelight;
font-size: 16px;
font-weight: bold;
text-decoration: none;
color: #1E5FB5;
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`);
_header.contentTypePDF();
_out.copy(pdfFile.input());
icon = _app.file("public/images/icon.png")
pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto
}
div.sidebar {
position: fixed
top: 0
bottom: 0
left: 0
width: 100px
background: #1B5886
}
div.sidebar > div {
position: absolute
top: 350px
transform: rotate(-90deg) translate(-50%, -34px)
font-family: Limelight
font-size: 40px
color: #89CFF1
}
main {
padding-left: 150px
}
h1 {
margin: 0 auto
text-align: center
font-family: Limelight
color: #BA250E
}
table {
margin: 0 auto
border: 1px solid black
border-spacing: 0
border-collapse: collapse
}
table > thead {
background: #40712A
color: #fff
}
table td, table th {
padding: 5px
text-align: center
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1
}
a {
font-family: Limelight
font-size: 16px
font-weight: bold
text-decoration: none
color: #1E5FB5
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`)
_header.contentTypePDF()
_out.copy(pdfFile.input())
icon = _app.file("public/images/icon.png")
pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto
}
div.sidebar {
position: fixed
top: 0
bottom: 0
left: 0
width: 100px
background: #1B5886
}
div.sidebar > div {
position: absolute
top: 350px
transform: rotate(-90deg) translate(-50%, -34px)
font-family: Limelight
font-size: 40px
color: #89CFF1
}
main {
padding-left: 150px
}
h1 {
margin: 0 auto
text-align: center
font-family: Limelight
color: #BA250E
}
table {
margin: 0 auto
border: 1px solid black
border-spacing: 0
border-collapse: collapse
}
table > thead {
background: #40712A
color: #fff
}
table td, table th {
padding: 5px
text-align: center
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1
}
a {
font-family: Limelight
font-size: 16px
font-weight: bold
text-decoration: none
color: #1E5FB5
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`)
_header.contentTypePDF()
_out.copy(pdfFile.input())
val icon = _app.file("public/images/icon.png")
val pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto
}
div.sidebar {
position: fixed
top: 0
bottom: 0
left: 0
width: 100px
background: #1B5886
}
div.sidebar > div {
position: absolute
top: 350px
transform: rotate(-90deg) translate(-50%, -34px)
font-family: Limelight
font-size: 40px
color: #89CFF1
}
main {
padding-left: 150px
}
h1 {
margin: 0 auto
text-align: center
font-family: Limelight
color: #BA250E
}
table {
margin: 0 auto
border: 1px solid black
border-spacing: 0
border-collapse: collapse
}
table > thead {
background: #40712A
color: #fff
}
table td, table th {
padding: 5px
text-align: center
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1
}
a {
font-family: Limelight
font-size: 16px
font-weight: bold
text-decoration: none
color: #1E5FB5
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`)
_header.contentTypePDF()
_out.copy(pdfFile.input())
final icon = _app.file("public/images/icon.png")
final pdfFile = _pdf.fromHTML(`
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Limelight&family=Roboto" rel="stylesheet">
<style>
* {
font-family: Roboto
}
div.sidebar {
position: fixed
top: 0
bottom: 0
left: 0
width: 100px
background: #1B5886
}
div.sidebar > div {
position: absolute
top: 350px
transform: rotate(-90deg) translate(-50%, -34px)
font-family: Limelight
font-size: 40px
color: #89CFF1
}
main {
padding-left: 150px
}
h1 {
margin: 0 auto
text-align: center
font-family: Limelight
color: #BA250E
}
table {
margin: 0 auto
border: 1px solid black
border-spacing: 0
border-collapse: collapse
}
table > thead {
background: #40712A
color: #fff
}
table td, table th {
padding: 5px
text-align: center
}
table > tbody > tr:nth-child(even) {
background: #e1e1e1
}
a {
font-family: Limelight
font-size: 16px
font-weight: bold
text-decoration: none
color: #1E5FB5
}
</style>
</head>
<body>
<div class="sidebar">
<div>Demonstration</div>
</div>
<main>
<h1>PDF from HTML</h1>
<div style="text-align: center; margin: 30px 0;">
<img width="50" src="${icon.uri()}">
</div>
<p>
This is an example HTML for generating a PDF file.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
tellus arcu, pharetra quis condimentum eget, dapibus convallis metus.
</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thread>
<tbody>
<tr>
<td>Maria Luísa</td>
<td>23</td>
</tr>
<tr>
<td>José Matos</td>
<td>38</td>
</tr>
</tbody>
</table>
<hr style="margin-top: 50px; height: 1px; background: #50B6C6; border: none;"/>
<div>
<a href="https://netuno.org" target="_blank">Netuno.org</a>
</div>
</main>
</body>
</html>
`)
_header.contentTypePDF()
_out.copy(pdfFile.input())
Videos
Below are some example videos on how to generate PDF files programmatically: