Load PDF from MS SQL Server and display it with MBS Plugins

We got this sample code for a client combining an example for MS SQL Server with SQLDatabaseMBS class and some sample code for rendering a picture via DynaPDFMBS class from PDF data. Please note that we use ImportPDFPage to only import the page we like to render.

Dim con As New SQLDatabaseMBS
		
Try
	
	// we used Microsoft SQL Server via ODBC data source and run app on Windows to test this
	
	con.Option("UseAPI") = "ODBC"
	con.DatabaseName = "ODBC:test"
	con.RaiseExceptions = True // if you like to get exceptions instead of checking error property
	con.Scrollable = False // disabling scrolling cursors is much faster for Microsoft SQL Server...
	
	con.Connect
	
	// go to database test
	con.SQLExecute "use test"
	
	// select PDFData from test table via some ID
	Dim p As SQLPreparedStatementMBS = con.prepare("SELECT PDFData FROM dbo.test WHERE ID=:ID")
	p.Bind("ID", 12345)
	Dim r As RecordSet = p.SQLSelect
	
	If r.EOF Then
		MessageBox "Not found"
	Else
		Dim data As String = r.Field("PDFData").StringValue
		
		Dim pdf As New DynaPDFMBS
		
		// For this example you can use a Pro or Enterprise License
		pdf.SetLicenseKey "Pro"
		
		// create in memory
		Call pdf.CreateNewPDF Nil
		
		// set import flags
		Call pdf.SetImportFlags pdf.kifImportAsPage + pdf.kifImportAll
		
		// open the PDF file
		Call pdf.OpenImportBuffer(data)
		
		// import page to render
		Call pdf.ImportPDFPage(1)
		
		// and render first page
		Dim out As Picture = pdf.RenderPagePicture(1)
		
		// display in window
		window1.Canvas1.Backdrop = out
		
		r.MoveNext
	End If
	
catch r as SQLErrorExceptionMBS
	
	// show error message
	MsgBox r.message
	
end try

Hi Christian,
How can I display the PDF in a web application?
note I use the 2019r32 version.
Thank you

Well, e.g. load into WebHTMLViewer using WebFile.

Or use plugin functions to render pages as jpg and show these in an WebImageView.

I had already tried using WebFile, but I had forgotten to run
Call pdf.CloseFile.
So the page was empty.
now it works.
Thank you.

Hi Christian,
another question:

the pdf saved in SqlServer’s varbinary field is digitally signed and contains attached data (xml).

But after downloading from HTMLViewer (CurrentFile.ForceDownload = True) the pdf seems to have lost the xml data and signature (I open it with Acrobat Reader).

on the other hand, if I create the pdf from SqlServer’s varbinary field (for example with DBeaver), Acrobat Reader shows the attached data and signature regularly.

are there specific settings in DynapdfMBS to show and download the correct file from binary?

Thank you

Please verify whether all bytes are actually downloaded to the browser.
Maybe some error in between cuts away a portion of the pdf.

PS: For a new different question, please always make a new topic here.

I assume that is only simple proof of concept code.

I created a fuller PDF viewer for PDFs stored in a PostgreSQL database for a larger project.

I did not use Canvas.Backdrop because I wanted to be able to zoom pages and scroll/drag within them so just drew he picture in the paint event.

In general I think to get the best looking pages on screen (particularly with zooming) you want to use
RenderPagePicture with all it’s parameters to get the best resolution for the size of the page being displayed.

To support page rotation (portrait ↔ landcape) I have been rotating the resulting picture outside of dynaPDF…

But in theory I think one may be able to do it using the matrix parameter of RenderPagePicture… But there is no example of doing that and I’m not sure which is faster so I have not tried.

Normally I would just write the PDF to a file and launch it and use a viewer app on the machine with no plugins required…

But in this case I needed to ensure the end user could not get a copy of the file or print it, so i needed to create the PDF in memory and display it myself to ensure that.

-Karen