Skip to content

Instantly share code, notes, and snippets.

@giridharprakash
Created August 21, 2017 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save giridharprakash/f6f5876d1459419d5a7fa6237d3466da to your computer and use it in GitHub Desktop.
Save giridharprakash/f6f5876d1459419d5a7fa6237d3466da to your computer and use it in GitHub Desktop.
Load pdf stream and show it in ionic app.
import {File} from '@ionic-native/file';
import { DocumentViewer } from '@ionic-native/document-viewer';
export class DocumentViewer
{
constructor(private file : File, private documentViewer: DocumentViewer)
{
}
public openDocument(document: Document)
{
if (!document)
{
this.showError("Document selected is null");
return;
}
let path "documents/"+databaseId;
let headers = new Headers({
'Accept': 'application/pdf'
});
let options = new RequestOptions({headers, responseType: ResponseContentType.ArrayBuffer});
let observableResponse = this.http.get(appConstants.apiUrl+
path, options);
observableResponse.subscribe((response: Response) => {
this.showDocument(response, document);
}, (error: any) => {
});
}
private showDocument(response: Response, document: Document)
{
let buffer = response.arrayBuffer();
let pdfBlob = new Blob([buffer], {type: 'application/pdf'});
this.file.writeFile(this.file.dataDirectory, document.databaseId+".pdf", pdfBlob, {replace: true}).then(c => {
this.documentViewer.viewDocument(this.file.dataDirectory+document.databaseId+".pdf", "application/pdf",
{print: {enabled: true}, bookmarks: {enabled: true}, email: {enabled: true}, title: document.title});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment