Upcoming Version 1.01
For my purposes I need to be able to generate a PDF report, 2. Save the pdf report to the server and 3. view the report on the browser. So we have added PDFView.
This has been further discussed
here.
1. We have added an element that we want to render to
Code:
body.Append($"<iframe id="basic"></iframe>"$)
2. We generate the PDF document as we usually do however instead of .Download, we use .Upload which expects a callback.
Let's take a simple example.
Code:
Sub display
maker.Initialize
maker.AddString("First paragraph")
maker.AddString("Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines")
'
maker.Upload(Me, "doUpload", "basic.pdf")
End Sub
The upload code generates the
FormData object that we need to upload using Ajax. This gets the .
Blob object of the PDF document so that we can save it. We pass this the filename that we want to save our PDF as.
Let's look at doUpload...
Code:
Sub doUpload(fd As BANanoObject)
Dim res As Map = BANano.FromJson(BANano.CallAjaxWait("http://localhost/demo/assets/upload.php", "POST", "application/pdf", fd, False, Null))
Dim status As String = res.Get("status")
Select Case status
Case "success"
Dim view As PDFView
view.Initialize("basic").SetHREF("./assets/basic.pdf").SetWidth(800).SetHeight(500).view
Case "error"
End Select
End Sub
doUpload executes an Ajax call to upload the file to the server. This returns a map with {status:success} or {status:error}, depending on that we call the PDFView to display the PDF document in our browser.
A loop at upload.php
Code:
<?php
header("Access-Control-Allow-Origin: *");
set_time_limit(0);
if (isset($_FILES['upload'])) {
// Example:
if(move_uploaded_file($_FILES['upload']['tmp_name'], "../assets/" . $_FILES['upload']['name'])){
echo '{ "status": "success" }';
} else {
echo '{ "status": "error" }';
}
exit;
} else {
echo '{ "status": "error" }';
}
?>
The php file expects an object that meets the file structure. This should have the name "
upload". Let's see how this was done.
1. You call the
maker.Upload sub.
Code:
'upload the file
Sub Upload(module As Object, methodName As String, fileName As String)
eventHandler = module
eventMethod = methodName
fName = fileName
'get the blob
Dim data As Object
GetBlob(Me, "forUpload", data)
End Sub
2. This calls the
GetBlob internal sub which falls back on the
forUpload callback
GetBlob, builds the pdf and run runs the getBlob method of pdfmake which falls back to forUpload call.
Code:
'get the blob of the document
Sub GetBlob(module As Object, methodName As String, data As Object)
Dim ddy As Map = getDD
Dim gd As BANanoObject = BANano.CallBack(module, methodName, Array(data))
pdfMake.RunMethod("createPdf", ddy).RunMethod("getBlob", Array(gd))
End Sub
3. forUpload, receives the Blob object receives and that is what we need to prepare for upload and convert in into FormData.
Code:
private Sub forUpload(data As Object)
Dim fd As BANanoObject = PrepareToUpload(data, fName)
BANano.CallSub(eventHandler, eventMethod, fd)
End Sub
4. We call PrepareForUpload.. (this prepares our blob for upload as a formdata receiving the file name also.
Code:
'prepare to upload, returns formData
Sub PrepareToUpload(blob As Object, fileName As String) As BANanoObject
Dim formData As BANanoObject
formData.Initialize2("FormData",Null)
formData.RunMethod("append", Array("upload", blob, fileName))
Return formData
End Sub
5. The result of this formdata is passed to be received by our callback receiving method in our case
doUpload
doUpload receives the FormData created by PrepareForUpload, executes the php on it to be received by the server and then display it.
Ta!