PDF attachments
In this tutorial we will show you how to modify the PhotoAttachmentQueue for PDF attachments.
Introduction
The current version of the To-Do List demo app implements a PhotoAttachmentQueue
class which
enables photo attachments (specifially a jpeg
) to be synced. This tutorial will guide you on the changes needed to support PDF attachments.
An overview of the required changes are:
- Update the app schema by adding a
pdf_id
column to the todos table to link a pdf to a to-do item. - Add a
PdfAttachmentQueue
class - Initialize the
PdfAttachmentQueue
class
The following pre-requisites are required to complete this tutorial:
- Clone the To-Do List demo app repo
- Follow the instructions in the README and ensure that the app runs locally
- A running PowerSync Service and Supabase (can be self-hosted)
- Storage configuration in Supabase
Steps
Usage Example
The newly created attachmentPdfQueue
can now be used in a component by using the useSystem
hook created in step-3 above
The code snippet below illustrates how a pdf could be saved when pressing a button. It uses a DocumentPicker UI component
to allow the user to select a pdf. When the button is pressed, savePdf
is called.
The saveAttachment
method in the PdfAttachmentQueue
class expects a base64 encoded string. We can therefore use
react-native-fs to read the file and return the base64 encoded string which is passed to saveAttachment
.
If your use-case generates a pdf file, ensure that you return a base64 encoded string.
Notes
Although this tutorial adds a new pdf_id
column, the approach you should take strongly depends on your requirements.
An alternative approach could be to replace the photo_id
with an attachment_id
and have one AttachmentQueue
class that handles all attachment types instead of having a class per attachment type.
Was this page helpful?