How to open Non-PDF document without converting to PDF in iOS?

Product: PDFTronAndroid SDK

Product Version: Demo

Please give a brief summary of your issue:How to open Non-PDF document without converting to PDF in iOS?
(Think of this as an email subject)

I would like to open image or text files in PDFViewCtrl class without converting to PDF.
Please suggest me how could i achieve this?

My steps are:
I am using SwiftUI to load the document https://www.pdftron.com/blog/ios/how-to-add-a-pdf-viewer-using-swiftui/.
I am downloading the file to iOS cache directory & from the path i am trying to load the document. But the issue is, the document is loading blank for the first time & if i load the same document again then it’s loading.

here is my code sample, Please let me know where do i
need correction:

if let url = url {
documentViewController.openDocument(with: url)
}

Thanks.

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

Please provide a link to a minimal sample where the issue is reproducible:

1 Like

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi @Manoj,

Thank you for getting in touch with us about this.

Are you able to share a minimal working sample project we can use to reproduce the behaviour you’re seeing? Can you share the file you’re having trouble with?
Is there a reason you are downloading the document first? The openDocument(with:) API should be able to handle remote documents directly.

Hi ,

Please find Code for above scenario:

struct ActivityIndicatorView: UIViewRepresentable {
typealias UIViewType = UIActivityIndicatorView
@Binding var animate: Bool

func makeUIView(context: Context) -> UIActivityIndicatorView {
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.hidesWhenStopped = true
    return activityIndicator
}

func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
    animate ? uiView.startAnimating() : uiView.stopAnimating()
}

}

struct PDFTronView: UIViewControllerRepresentable {

var url: URL?

func makeUIViewController(context: Context) -> PTDocumentController {
    // Create and return a new PTDocumentViewController instance.
    let documentViewController = ViewerViewController()

    // Check if there was a URL specified.
    if let url = url {
        // Open the document at the specified URL.
        documentViewController.openDocument(with: url)
    }
    
    return documentViewController
}

func updateUIViewController(_ uiViewController: PTDocumentController, context: Context) {
    DPrint("updateUIViewController")
}

}

class ViewerViewController: PTDocumentController {

override init() {
    super.init()
}

required init(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

struct DocumentPreviewView: View {
@ObservedObject var viewModel: DocumentPreviewViewModel

var body: some View {
    switch viewModel.state {
    case .idle:
        Color.clear.onAppear(perform: viewModel.fetchDocument)
    case .loaded(let url):
        PDFTronView(url: url)
    case .failed(_):
        ActivityIndicatorView(animate: .constant(false))
    case .loading:
        ActivityIndicatorView(animate: .constant(true))
    }
}

}

On DocumentPreviewViewModel we are trying to fetch the file from Server and Save it in cache dir
and then passing the URL.

final class DocumentPreviewViewModel: ObservableObject {

enum State {
    case idle
    case loading
    case failed(String)
    case loaded(URL)
}

@Published private(set) var state = State.idle

private var localFileUrl: URL?
private let remoteFileUrl: String?
private let documentFile: DownloadableFile?

init(document: DocumentMO) {
    localFileUrl = document.documentFileUrl
    remoteFileUrl = document.fileURL
    documentFile = document.loadFileIfNeeded()
}

func fetchDocument() {
    state = .loading
    if let fileUrl = localFileUrl {
        state = .loaded(fileUrl)
    } else {
        documentFile?.downloadedPath?.bind(observer: { [weak self] (_, path) in
            if let docUrl = URL(string: path) {
                self?.localFileUrl = docUrl
                DispatchQueue.main.async {
                    self?.state = .loaded(docUrl)
                }
            }
        })
        documentFile?.errorMessage?.bind(observer: { [weak self] (_, message) in
            print("Error \(message)")
            DispatchQueue.main.async {
                self?.state = .failed(message)
            }
        })
    }
}

deinit {
    documentFile?.downloadedPath?.removeObservers()
    documentFile?.errorMessage?.removeObservers()
}

}

Hi @saikat.guchhait,

Are you able to share a full working sample Xcode project (and file) which we can run locally to reproduce the behaviour?

Also just to clarify, any image or text files you open in the viewer will be converted to PDF files in order to be displayed.

Are you able to download and run one of the sample projects in the full SDK DMG and confirm if the file you are trying to open is displayed there? For example the SwiftSample project in that download.

Hi ,

I am able to resolve this issue. Now I am able to load the local files in PDFtron View.

Thanks