How to view a PDF in a html viewer, not from a file

Hi I have a PDF created with DynaPDFMBS, and normaly I create a PDF file on disk and view this in preview (on mac).

But I don’t always want to create/write this file. sometimes I just want to see it…

Can I use htmlviewer and just send my pdf to that ? without writing a file first to disk ?

/helge

I have done this recently, but because of persistent issues with standard font settings and irregular output to the HTMLviewer, I eventually rewrote my reports so that they were HTML code instead of PDFs and sent that to the HTMLViewer. That has been a much more reliable solution for me personally.

[quote=229487:@Helge Tjelta]Hi I have a PDF created with DynaPDFMBS, and normaly I create a PDF file on disk and view this in preview (on mac).

But I don’t always want to create/write this file. sometimes I just want to see it…

Can I use htmlviewer and just send my pdf to that ? without writing a file first to disk ?

/helge[/quote]
I don’t know how you can do it with a HTMLViewer, but if you’re using a Mac, you can always stuff the PDF data into a NSImage and then draw that NSImage on screen.

Why don’t you always write it in the Temporaty Items folder, then move it or not where you need it ?

you can use DynaPDF to render pages as images and show them in canvas for example.

or you use one of the htmlviewer methods to load PDF from string.
e.g. HTMLViewer.LoadHTMLStringMBS with passing right mime type and PDF data.

Hi Christian, do you have and example for the last one… loadHTMLString… mine says (text as string, url as string…)

But the PDF is not a file, only as a MyDynaPDFMBS

I sent you an example “Create PDF and show in HTMLViewer.rbp”
maybe it helps.

  1. Does this work on both Windows and Mac?
  2. Does this work without having a PDF reader already installed on the system? (loading a PDF in a browser uses Adobe’s Reader plugin if that is installed, but otherwise may fail).
  3. Could you post the code here please?

Thanks.

Well, to answer my own questions:

  1. No. MBS docs do clearly state that LoadHTMLStringMBS is Mac-only.
  2. Yes. I tried it using .pdf files.
  3. Code is as simple as:
    dim pdfData as MemoryBlock = GetTextFileContents( f )
    HTMLViewer1.LoadHTMLStringMBS( pdfData, "application/pdf", "", "" )

… where GetTextFileContents is an IOException-safe method that does just what it says: opens a file and reads the contents in a TextInputStream, returning the result as a string.

Result:
The display in the browser is the same as in Safari.

So next question: would HTMLViewer.IELoadHTMLMBS also do this on Windows? (Can’t test it myself because it isn’t available in my registered version of the MBS plugin, and I don’t want to install plugin versions I can’t actually use in my deployed apps.)

I guess the answer is “no”. Considering that the result above for Mac is the same as using the native Xojo LoadPage for a .pdf file, on Windows loading a pdf in the browser will only work if a pdf browser plugin has been installed by the user, and even then it may fail.

Well, if you really want cross platform rendering without requiring a browser plugin, than please use DynaPDF.

But on Mac you can pass PDF with LoadHTMLStringMBS.
While we do have functions to load strings for Linux/Windows, I didn’t try those.
It may be required to use a data URL for this.

But in general you may have the problem on Windows that there is no PDF plugin when using WebKit rendering there.

Please do not read PDF files with TextInputStream!
This can corrupt the data. Use Binarystream, please.

[quote=229998:@Christian Schmitz]Please do not read PDF files with TextInputStream!
This can corrupt the data. Use Binarystream, please.[/quote]

Okay; thank you.

  dim f as FolderItem = GetOpenFolderItem( FileTypes1.pdf ) ' you'll need to define the file type
  if f <> Nil and f.exists then
    dim pdfData as MemoryBlock = GetBinaryFileContents( f )
    HTMLViewer1.LoadHTMLStringMBS( pdfData, "application/pdf", "", "" )
  end if
Function GetBinaryFileContents(f as FolderItem) As memoryblock
  dim stream as binaryStream
  dim s as string
  
  // LOAD BINARY FILE
  if f <> Nil and f.exists then
    stream = SafeBinaryStreamOpen(f)
    if stream <> Nil then
      // GET BYTES
      s = stream.Read( stream.Length )
      stream.close
    end if
  end if
  
  // FILL MEMORYBLOCK
  dim m as MemoryBlock
  if s.len > 0 then 
    m = new MemoryBlock(s.len)
    m.StringValue(0, s.len) = s
  end if
  
  return m
  
End Function
Function SafeBinaryStreamOpen(f as folderitem) As BinaryStream
  // BinaryStream.Open( f ) can cause annoying incomprehensible IOExceptions
  // Instead of using try blocks in every situation that uses BinaryStream.Open, 
  // use this method which will simply return a Nil stream instead of throwing an exception
  
  dim stream as BinaryStream
  dim caughtIOException as Boolean
  
  ' try to open the file
  try
    stream = BinaryStream.Open(f)
  catch err as IOException
    caughtIOException = true
  end try
  
  ' exception?
  if caughtIOException then return Nil else return stream
  
End Function

For people who do not need to do much more than display PDFs cross platform, DynaPDF is absurdly expensive.

I have the DynaPDF “Lite” license which is useful, but already cost me $300, and doesn’t allow me to render PDFs. To render, one has to pay twice more and then some to get the “Pro” license. I find that really incredible.

The authors should offer a reasonably-priced cross-platform rendering license, which has one function working on both Mac and Windows: pdf.render Their tool does a lot more, but most people don’t need all that. A render function is not worth the price they are asking.

Well, the license rules are not mine.

[quote=230113:@Aaron Hunt]I have the DynaPDF “Lite” license which is useful, but already cost me $300, and doesn’t allow me to render PDFs. To render, one has to pay twice more and then some to get the “Pro” license. I find that really incredible.

The authors should offer a reasonably-priced cross-platform rendering license, which has one function working on both Mac and Windows: pdf.render Their tool does a lot more, but most people don’t need all that. A render function is not worth the price they are asking.[/quote]
Have you contacted Microsoft and requested that they support PDF at an OS level, I don’t understand why they still don’t.

Do PDFs display in Internet Explorer? Or ‘Edge’?

[quote=230256:@Sam Rowlands]Have you contacted Microsoft and requested that they support PDF at an OS level, I don’t understand why they still don’t.

Do PDFs display in Internet Explorer? Or ‘Edge’?[/quote]

Windows developers feedback is full of requests for PDF system support. Maybe the “new” microsoft will listen at long last.

They are on their way, though. Edge does support PDF. As for IE, it has been a lost cause for quite a while and is probably on its way exit stage, floor left. It still needs the Adobe plugin to display PDF.

If you can use the Edge browser somehow within Xojo, then you could probably use that to display PDFs. I know it doesn’t help customers using an older version of Window, but at least it’s an option.