I want to show a PDF file generated with pdfMake with the nativescript-pdf-view
plugin in a NativeScript-Vue app.
I am unable to show the file inside the plugin widget. I started from this example, but I want to display the file inside the app.
First I generate encode the PDF to base64, which works fine, then I write the encoded string to file. When I pass the file path to the widget nothing shows up. URLs are shown correctly, so the error might happen while writing to file or handling the path.
PDF generation (app/scripts/pdf.js):
import { createPdf } from "pdfmake/build/pdfmake";
import { pdfMake } from "pdfmake/build/vfs_fonts";
import { knownFolders } from "tns-core-modules/file-system";
export default {
generatePdf() {
return new Promise((resolve, reject) => {
var docDefinition = {
pageSize: "A5",
pageMargins: [40, 40, 40, 40],
content: { text: "Test", fontSize: 80 }
};
var file = knownFolders.documents().getFile("document1.pdf");
createPdf(docDefinition, "", "", pdfMake.vfs).getBase64(base64 => {
// decoding this string returns a correct pdf file
file.writeText(base64).then(() => {
// file properties are updated
resolve(file.path);
// path example: "/data/user/0/{app package}/files/document1.pdf"
}).catch(e => reject(e));
});
});
},
// other code
}
Component:
<template>
<GridLayout rows="* *">
<Button row="0" @tap="getPDF" text="get pdf" />
<PDFView row="1" :src="pdf" />
</GridLayout>
</template>
<script>
import pdfModule from "../scripts/pdf";
export default {
data() {
pdf: ""
},
methods: {
async getPDF() {
this.pdf = await pdfModule.generatePdf().catch(e => console.error(e));
}
}
}
</script>