Couchbase lite attachment URL

112 views
Skip to first unread message

Shizam

unread,
Aug 15, 2015, 8:56:08 PM8/15/15
to Couchbase Mobile
Hi,
    I have the following use case that I am trying to resolve but not sure if the couchbase lite API supports it. My iOS app lets users record textual and image information that is stored on a couchbase lite database on the user's mobile device. The app lets users add/view/edit/delete information, all of which works beautifully using couchbase lite. The app offers the user a feature to export all the data to a PDF file that should be generated locally (on the user's mobile) so that the user can email the PDF. Here is where I am stuck with issues in accessing the image data (stored as a document attachment) to render it on the PDF.

    My approach thus far has been to access the image data as raw bytes using Attachment.content and render it on a UIWebView as a base64 encoded image and then get the UIWebView object to export the HTML data as a PDF (all in memory). This is turning out to take a long time (takes in excess of 10 secs for just a single image) and is CPU intensive. The delay appears to be occurring in UIWebView in rendering the HTML with base 64 image data.

    So my question is if there is a way to access an attachment in couchbase lite as a direct URL (like file:///....)  so that one can circumvent the entire base64 conversion and rendering stages.

    Any help will be deeply appreciated.

Regards
S
   

Jens Alfke

unread,
Aug 15, 2015, 9:02:51 PM8/15/15
to Couchbase Mobile

On Aug 15, 2015, at 4:57 PM, Shizam <sri...@simplemachines.com.au> wrote:

    My approach thus far has been to access the image data as raw bytes using Attachment.content and render it on a UIWebView as a base64 encoded image and then get the UIWebView object to export the HTML data as a PDF (all in memory).

That seems like a roundabout way of doing it! CoreGraphics has a whole API for reading and writing PDFs — take a look at CGPDFContext. I’ve only used this for reading PDF files, but it looks like it goes the other way too, allowing you to generate PDFs.

    So my question is if there is a way to access an attachment in couchbase lite as a direct URL (like file:///....)  so that one can circumvent the entire base64 conversion and rendering stages.

Hopefully you won’t need this :) but yes — use CBLDatabase.internalURL to get the root URL of the database in the REST API, then append path components for the docID and the attachment name. The resulting URL can be used inside the app (including in a UIWebView) to read the attachment.

—Jens

Brendan Duddridge

unread,
Aug 15, 2015, 10:33:25 PM8/15/15
to Couchbase Mobile
Perhaps I'm misunderstanding something, but isn't CBLAttachment's contentURL designed to give you the URL to the file attachment?

Brendan

Shizam

unread,
Aug 16, 2015, 2:43:29 AM8/16/15
to Couchbase Mobile
Thanks so much for the response. That was exactly what I was looking for and am now able to render the image in a PDF. The reason, I opted to go with UIWebView and then export the HTML to PDF is that this way I get to leverage the formatting and table and image  rendering in already present in UIWebView.

All this time I have been looking at the documentation at http://developer.couchbase.com/mobile/develop/references/couchbase-lite/index.html and it doesn't mention the existence of the CBLDatabase.internalUrl property.

For others looking for a similar solution, I ended up doing the following (in Swift)
let dbUrl = doc.database.internalURL
let url
= dbUrl.absoluteString?.stringByAppendingPathComponent(doc.documentID).stringByAppendingPathComponent(imageFileName)



Thanks again!!
S

Shizam

unread,
Aug 16, 2015, 2:44:22 AM8/16/15
to Couchbase Mobile
Deprecated I believe.

Jens Alfke

unread,
Aug 16, 2015, 3:34:27 AM8/16/15
to Couchbase Mobile

On Aug 15, 2015, at 11:43 PM, Shizam <sri...@simplemachines.com.au> wrote:

All this time I have been looking at the documentation at http://developer.couchbase.com/mobile/develop/references/couchbase-lite/index.html and it doesn't mention the existence of the CBLDatabase.internalUrl property.

It’s not part of the official cross-platform API. There’s a fair bit of stuff that isn’t. If you want to see what’s available, your best bet is to look at the header files, or at the API docs auto-generated from the headers.

—Jens

Todd Freese

unread,
Aug 17, 2015, 11:04:59 AM8/17/15
to Couchbase Mobile
Another option that works really well for me is to use PaintCode to layout your reports, then use CoreGraphics to make the PDF. Super simple and has a great workflow.

Todd
Reply all
Reply to author
Forward
0 new messages