Embed PDFs and Document Links in LibreOffice Base
LibreOffice Base allows users to manage and access external documents, such as PDFs, directly from database forms. Instead of storing large binary files within the database itself—which causes file bloat and performance issues—the optimal method is to store local file paths or URLs in text fields. By leveraging standard form controls, the LibreOffice system integration service, or lightweight macros, you can create functional document links and trigger external PDF viewers directly from your Base user interface.
1. Prepare the Table to Store File Paths
Before configuring form controls, ensure your underlying database table has a dedicated field for storing the location of your external files.
- Open your database in LibreOffice Base and navigate to Tables.
- Edit your table in Design View.
- Add a field named
DocumentPath(or similar). - Set the Field Type to
Text [VARCHAR]and assign a length sufficient for full file paths (e.g., 255 or 500 characters). - Save and close the table design.
Paths should ideally be formatted as standard operating system file
paths (e.g., C:\Documents\Invoice.pdf on Windows or
/home/user/Documents/Invoice.pdf on Linux/macOS) or as file
URLs (e.g., file:///C:/Documents/Invoice.pdf).
2. Create an External Link Button on the Form
The most straightforward way to open an external PDF using the operating system’s default viewer is by adding a configured Push Button.
- Open your Form in Design Mode.
- Insert a Text Box linked to the
DocumentPathfield so the path for the current record is accessible. - Select the Push Button tool from the Form Controls toolbar and draw a button on the form.
- Right-click the button and select Control Properties.
- In the General tab:
- Set Label to
Open PDF. - Scroll down to Action and change it to
Open document/web page. - For static links, enter the direct file path into the URL property.
- Set Label to
3. Dynamically Open PDFs Using a Macro
Because form records typically link to different files, a macro is required to dynamically read the active record’s file path and launch it.
Step A: Add the Macro
- Go to Tools > Macros > Organize Macros > Basic.
- Select your database file, click New, and paste the following script:
Sub OpenLinkedPDF(oEvent As Object)
Dim oButton As Object
Dim oForm As Object
Dim sDocPath As String
Dim oShell As Object
oButton = oEvent.Source.Model
oForm = oButton.Parent
' Retrieve the path from the DocumentPath column
sDocPath = oForm.getString(oForm.findColumn("DocumentPath"))
If sDocPath <> "" Then
' Convert system path to URL format if necessary
sDocPath = ConvertToURL(sDocPath)
' Launch the file using the default system handler
oShell = createUnoService("com.sun.star.system.SystemShellExecute")
oShell.execute(sDocPath, "", 0)
Else
MsgBox "No file path specified for this record.", 48, "Missing Path"
End If
End SubStep B: Bind the Macro to the Button
- Open the form in Design Mode.
- Right-click the Push Button and open Control Properties.
- Under the General tab, set Action
to
None. - Switch to the Events tab.
- Click the browse (
...) button next to Execute action, select Macro, locateOpenLinkedPDF, and click OK. - Save the form and exit Design Mode. Clicking the button will now open the corresponding PDF for the active record.
4. Embedding In-Form PDF Previews
LibreOffice Base does not include a native interactive PDF rendering control. However, there are two practical approaches to visual previews within forms:
- Image Control for Converted PDFs: If visual confirmation on the form is mandatory, convert the first page of the PDF to an image format (PNG/JPEG). Store that image path in the database and bind it to an Image Control on the form, which natively renders images inside LibreOffice Base.
- External Active Viewing: Relying on the
SystemShellExecutemacro allows the operating system’s dedicated PDF viewer (such as Adobe Acrobat, Preview, or a web browser) to handle search, zooming, and printing alongside the Base interface.