PDF file as WebFile


I did realized a small webapp based on the code described here: https://documentation.xojo.com/api/web/webfile.html

Now I would lite to do the same with a PDF file created and downloaded on the fly.
I am working with pieces of code found here: https://blog.xojo.com/2020/08/27/pdf-arrives-at-xojo/

No problem to create the PDF file and its grapics content, but I can’t find a way to download it as WebFile like explained in the first link.

Can someone put me on the right direction?

Look for the “EE-PDF-Web.xojo_binary_project” in the Xojo PDF Samples Folder.


and if that code is too complicated, create a new web app, create a button and the variable myWebFile as shown here:

In the pressed event of your button add the following code:

// Creating your Pdf
Var MyPDF As New PDFDocument(PDFDocument.PageSizes.A4)
Var g As Graphics = MyPDF.Graphics
g.DrawingColor = &C00ff00
Var x, y As Integer
Var size As Integer = 200
Var offset As Integer = size / 2
x = MyPDF.Graphics.Width / 2
y = MyPDF.Graphics.Height / 2
g.FillOval(x - offset, y - offset, size, size)
Var helloWorld As String = "Hello PDF World"
g.FontUnit = FontUnits.Pixel
g.FontSize = 20
g.Bold = True
g.DrawingColor = &cffffff
Var textWidth As Integer = g.TextWidth(HelloWorld) / 2
Var textHeight As Integer = (g.TextHeight / 2) - g.FontAscent
g.DrawText(helloWorld, x - textWidth, y - textHeight)

// Creating a new webfile and link it to the variable defined in the IDE
myWebFile = new WebFile

// Write into a temporary folder (ensure you have the necessary right
// on your remote server for the user on the server running the app
var f as folderitem = SpecialFolder.Temporary.child("mypdf.pdf")
// temporarily save your pdf
MyPDF.save( f )
// set the metadate of your webfile
myWebFile.MIMEType = "application/pdf"
myWebFile.ForceDownload = False
// load your physical temporary file into your webfile
myWebFile = webfile.Open( f)
// remove your temporary file

// Download your file to the user's computer
Var success as boolean = myWebFile.Download
#Pragma unused success

My code is a simple as possible, please be aware that you should handle possible exceptions. At my knowledge there is not yet a possibility with PdfDocument in Xojo to load the file in memory, so you have to store it somewhere. On webservers the temporary folder is ideal for this.

If you want to create a pdf in memory, I can tell that @Christian_Schmitz 's MBS Dynpro Plugin can do that.

<https://xojo.com/issue/63258> :wink:


Gracias! Great news.

Until then my proposal is then obviously the only way to solve your challenge. Please be really aware that my code needs much more love. This is only reduced to the absolute minimum. I/O exceptions (missing permissions, existing filenames, you name it) are very likely in temporary folders, so even if the code might run now, you have to handle all possible exceptions in your final code :wink: . The variable myWebFile needs to be defined in the properties, so that its context “survived” the end of the pressed event, otherwise you would get a 0 byte file downloaded.

1 Like

The only way I can take at the moment is little more clear to me now.
Thank you everybody.

1 Like

This was probably a bit unclear. If you don’t declare the variable in properties but directly in your code with a

Var myWebFile as WebFile

this variable will be Nil, before the framework started the download process, so it is important to declare it it a “level higher”. You could as well define it in the session section. It only must be declared somewhere, where it contents will survive longer than your code and variables in the “pressed” event section.