FPDF Output

Hi,

Is there any way to export a pdf file directly without this is open automatically …?
I had the idea that parameter “F” in “pdf.output” achieve this goal, but does not work. Every time that code is proccessed, the pdf document is generated and opened.

  dim pdf as fpdf
  dim f as FolderItem
  
  f=SpecialFolder.Desktop.Child("Test.pdf")
  pdf = new fpdf("P")
  pdf.AddPage()
  pdf.SetFont("Arial","B",16)
  pdf.Cell(40,10,"Your text")
  pdf.Output(f.AbsolutePath,"F")

I will be appreciate any suggestions to fix.

Thanks.

Instead of F as the destination, you could try S
In theory, that returns the PDF file as a string.
If you then save that string as a binarystream, it wont automatically launch.

maybe:

dim s as string s = pdf.Output("Ignored" ,"S") //now save the string to disc

Hi Jeff,

Thanks for your reply. I have tested with your suggestion, but with “S” the document is not generated although in teory the parameter “S” is correct according to fpdf instructions.

I’m testing the rpdf library that is based in fpdf, but I’m thinking that the problem is rpdf because the parameter “S” and “F” works without problem in web mode or with php.

With “F” parameter, the pdf document is generated but is opened inmediately, and with parameter “S”, the pdf document is not created.

Jean Paul is correct

The current source on github has this in the fpdf source

[code] case “F”
f = GetFolderItem(name)
t = f.CreateBinaryFile(“pdf”)
t.Write me.buffer
t.close
f.Launch

	  case "S"
	    //Return as a string
	    'return me.buffer[/code]

Remove the f.launch line.

And the reason the S doesnt work is that return me.buffer has been commented out, by the looks of it.

Hi Jeff, Jean,

Thanks for your suggestions, I will try with your code and I will comment my result later.

Jeff, Jean,

Finally the code is working. I have implemented some changes in “Output” method of fpdf library to get more flexibility.

01 - I added a new optional parameter to “Output” named “openpdf” with value false by default:

optional name as string = "", optional dest as string = "F", [b]optional openpdf as Boolean=False[/b]

02- For case “F” I have changed the line f.launch that now checks the parameter “openpdf (true/false):”

case "F" f = GetFolderItem(name) t = f.CreateBinaryFile("pdf") t.Write me.buffer t.close [b]if openpdf=True then f.Launch[/b]

03- The final code to generate the pdf document is as follow:
Now, the “pdf.Output” method expects a new parameter to decide if pdf document will be opened.

`
dim pdf as fpdf
dim f as FolderItem

f=SpecialFolder.Desktop.Child(“Test.pdf”)
pdf = new fpdf(“P”)
pdf.AddPage()
pdf.SetFont(“Arial”,“B”,16)
pdf.Cell(40,10,“Your text”)
pdf.Output(f.AbsolutePath,“F”, False)

MsgBox “Done”

`

Jeff, Jean, thank you so much for your ideas, I have solved my problem.

just out of curiosity, did you make fpdf working with images? it looks like that version on github (which is quite old actually) does not work with images…
Am I alone?

Hi Davide,

Yes, you can work with images using this code:

pdf.Image("yourpath\\yourimage.jpg",10,15,500,400)

Paremeters 2/3 are x, y positions
Parameters 4/5 are width, height image

I have included an image using “rpdf” and the document is generated without problem. Just keep on mind that is important optimize the weight of the image in order to get a balanced process when the pdf document is exported.

[quote=253064:@Elvis Agamez]Hi Davide,

Yes, you can work with images using this code:

pdf.Image("yourpath\\yourimage.jpg",10,15,500,400)

Paremeters 2/3 are x, y positions
Parameters 4/5 are width, height image

I have included an image using “rpdf” and the document is generated without problem. Just keep on mind that is important optimize the weight of the image in order to get a balanced process when the pdf document is exported.[/quote]

In my case it does not work. It stays there forever (the spinning wheel appears) but not pdf is produced…if I don’t use images everything works…
Which Xojo version are you using?

Thanks

Davide,

I’m working with RS and the last version of “rspdf” library published in github. Certeainly, with Xojo the code does not work.

I have made a test with Xojo and the problem is presented with the methods “GetImageSized” and “JpegNextMarker”, that are using the properties “binarystream.readbyte” and “binarystream.readshort”, not availables in Xojo.

I think that would be necessary readapt the code in Xojo to get use the library properly and wthout problems. You could try find the equivalents in Xojo for “binarystream.readbyte” and “binarystream.readshort”.

The equivalent are ReadInt8 and ReadInt16. It’s not that the problem with the images, maybe the encoding?

Davide I’m not sure , sorry. Perhaps other dev can help us to readapat the code for this library.
Just out of curiosity, what other alternatives have you considered to export pdf documents …?

It seems to be a Mac issue. I get the beach ball forever as well with RS 2012R1.2.

I managed to solve to problem by modifying the code. The png files are now working. Replace the code in “parsePNG” with the following. JPG is still not working. I will take a look at that when possible. One thing is not clear to me. If I have a 200x200 pixels png image and I use pdf.Image(“image.png”,10,15,200,200) I get a huge picture…what’s the size to be passed in that method?

  dim colspace,parms,pal,data,type as string
  dim w,h,n as double
  dim trns as Collection
  
  dim bpc,ct as Integer
  
  dim r as new Collection
  Dim f As FolderItem
  Dim t as BinaryStream
  
  dim nulo as string
  
  f = GetFolderItem(file,  FolderItem.PathTypeShell)
  t = f.OpenAsBinaryFile
  
  //Check signature
  if (t.Read(8, Encodings.UTF8) <> chrB(137) + "PNG" + chrB(13) + chrB(10) + chrB(26) + chrB(10)) then
    me.Error("Not a PNG file: " + file)
    return r
  end if
  
  //Read header chunk
  nulo = t.read(4)
  
  if(t.read(4) <> "IHDR") then
    me.Error("Incorrect PNG file: " + file)
    return r
  end if
  
  w    = t.ReadInt32
  h    = t.readint32
  
  bpc  = t.read(1).Asc
  
  if(bpc > 8) then
    me.Error("16-bit depth not supported: " + file)
    return r
  end if
  
  ct = t.read(1).asc
  
  select case ct
    
  case 0
    colspace ="DeviceGray"
  case 2
    colspace = "DeviceRGB"
  case 3
    colspace="Indexed"
  case else
    me.Error("Alpha channel not supported: " + file)
    return r
    
  end select
  
  if(t.read(1).asc <> 0) then
    me.Error("Unknown compression method: " + file)
    return r
  end if
  
  if(t.read(1).asc <> 0) then
    me.Error("Unknown filter method: " + file)
    return r
  end if
  
  if(t.read(1).asc <> 0) then
    me.Error("Interlacing not supported: " + file)
    return r
  end if
  
  nulo = t.Read(4)
  
  parms = "/DecodeParms <</Predictor 15 /Colors "
  
  if (ct = 2) then
    parms = parms + "3"
  else
    parms = parms + "1"
  end if
  
  parms = parms + " /BitsPerComponent " + str(bpc) + " /Columns " + str(w) + ">>"
  
  //Scan chunks looking for palette, transparency and image data
  pal  = ""
  trns = new collection
  data = ""
  
  do
    
    nulo = str(t.readint32)
    n = nulo.CDbl
    
    type = t.read(4)
    
    select case type
      
    case "PLTE"
      pal = t.read(n)
      nulo = t.Read(4)
      
    case "tRNS"
      
      //Read transparency info
      nulo = t.read(n)
      
      if(ct = 0) then
        
        trns.Add str(nulo.left(1).asc)
        
        
      elseif(ct = 2) then
        
        trns.Add str(nulo.mid(1,1).asc)
        trns.Add str(nulo.mid(3,1).asc)
        trns.Add str(nulo.mid(5,1).asc)
      else
        if (nulo.inStr(chr(0)) > 0) then
          trns.Add str(nulo.inStr(chr(0)))
        end if
      end if
      
      nulo = t.read(4)
      
    case "IDAT"
      
      //Read image data block
      data = data + t.read(n)
      nulo = t.read(4)
      
    case "IEND"
      
    case else
      
      nulo = t.read(n + 4)
      
    end select
    
  loop until(t.EOF)
  
  t.close
  
  if(colspace = "Indexed" AND  pal = "") then
    me.Error("Missing palette in " + file)
    return r
  end if
  
  r.Add w, "w"
  r.add h, "h"
  r.add colspace, "cs"
  r.add bpc, "bpc"
  r.add "FlateDecode", "f"
  r.add parms, "parms"
  r.add pal, "pal"
  r.add trns, "trns"
  r.add data, "data"
  
  return r

The docs say:

[quote]Width of the image in the page. There are three cases:
If the value is positive, it represents the width in user unit
If the value is negative, the absolute value represents the horizontal resolution in dpi
If the value is not specified or equal to zero, it is automatically calculated[/quote]

So since you are supplying a positive number, you are getting 200 * (user unit)
Now, I am not sure what that is or what you have it set to, but if it is mm or inches, then you are getting 200 pixels * <number of pixels needed to make a mm or an inch)

A negative value seems to set the horizontal resolution… so if you expect the 200 pixel image to print at 10 inches width, you would supply -20

What you get from a value of 0 seems to rely on the properties of the image: it has its own idea of dpi, and it ‘knows’ how many pixels it has, so the size you get is pixels/dpi

Where do I find the FPDF library for Xojo 2016 ??

Nop, it’s ReadUInt8 and ReadUInt16

i managed to make it work,

readInt8 return -1 when reading 0xFF : it supposed to return 255
readUInt8 return 255 :slight_smile:

Int8 (-128 to 127) so impossible to get 255
UInt8 (0 to 255) :wink:

What do you think i Fork this on GitHub ?