Jump to content
taietel

Create PDF from your application

Recommended Posts

happytc, have you tried the version from the post #95?

No, I'm not using FlatDecode in the UDF.

Yes, testing Example_Image2PDF.au3 in MPDF_UDF_102: F5 -> select image files -> get file Demo.pdf -> open by Acrobat reader

The screenshot Error info:

Edited by happytc

Share this post


Link to post
Share on other sites

Yes, testing Example_Image2PDF.au3 in MPDF_UDF_102: F5 -> select image files -> get file Demo.pdf -> open by Acrobat reader

As I see, you run the example that converts images to pdf and pick a pdf as image? :mellow: Is it not obvious why that error appear?

Try again, but WITH some REAL image files (jpg, bmp, png, gif, tif ...), not text files, and see if it works.

Share this post


Link to post
Share on other sites

As I see, you run the example that converts images to pdf and pick a pdf as image? :mellow: Is it not obvious why that error appear?

Try again, but WITH some REAL image files (jpg, bmp, png, gif, tif ...), not text files, and see if it works.

No, I selected real image file from my computer.

BTW:the example of Example_Txt2PDF.au3 in the MPDF_UDF_102 worked

Share this post


Link to post
Share on other sites

I'm sure it worked (Example_Txt2PDF.au3) because this one is an example for text file.

Post that image that didn't worked with the Example_Image2PDF.au3.

My guess is that you changed this line

$var = FileOpenDialog("Select images", @ScriptDir & "\", "Images (*.jpg;*.bmp;*gif;*png;*tif)", 1 + 4 )

by adding non-image files, hence the error.

Share this post


Link to post
Share on other sites

The image files i tested from your old examples.

I don't modify the Example_Image2PDF.au3.

now, i add a line: MsgBox(0,0,$var),it displays:

Share this post


Link to post
Share on other sites

Ok, now please post the corrupted pdf that resulted from running the example with the above pictures. I'll take a look at that.

I've tested (again) now and in the attachment is the result.

Demo.pdf

Edited by taietel

Share this post


Link to post
Share on other sites

Ok, now please post the corrupted pdf that resulted from running the example with the above pictures. I'll take a look at that.

I've tested (again) now and in the attachment is the result.

No problem to open your pdf file

Share this post


Link to post
Share on other sites

happytc, as I said,

...please post the corrupted pdf that resulted from running the example with the above pictures

I didn't said to post a screenshot!

I know is not a problem with that pdf and also know that you have tested with those images and it's fine too, except the first time when instead of an image you have chosen a pdf file.

Share this post


Link to post
Share on other sites

happytc, as I said,

I didn't said to post a screenshot!

I know is not a problem with that pdf and also know that you have tested with those images and it's fine too, except the first time when instead of an image you have chosen a pdf file.

Here the file!

I followed MPDF_UDF.au3, and found the function __ToBinary() had a problem: StringLen(BinaryToString($Bin)) = 2667, but msgbox(0,0,BinaryToString($Bin)) was only displayed: ?

msgbox(0,0,$Bin) is OK!

Func __ToBinary($sImage)

Local $hFile, $Bin

$hFile = FileOpen($sImage, 16)

$Bin = FileRead($hFile)

FileClose($hFile)

Return BinaryToString($Bin)

EndFunc

Share this post


Link to post
Share on other sites

happytc, the pdf that you have posted IS NOT CONTAINING any of the images you said you have tested with (or at least a fragment of them). If you changed the UDF without knowing what you are doing, it's your problem.

Share this post


Link to post
Share on other sites

happytc, the pdf that you have posted IS NOT CONTAINING any of the images you said you have tested with (or at least a fragment of them). If you changed the UDF without knowing what you are doing, it's your problem.

No, I haven't modified the functions. For proving this, I downloaded again from your post#95, and tried to test the function of Example_Image2PDF() again. The same error occured.

"(or at least a fragment of them)" ---- i don't know why it happened. I only did thing was following your example.

By the way, I found the tmp image file generated by the function of _LoadResImage() was OK.

Edited by happytc

Share this post


Link to post
Share on other sites

happytc, see this:

Here is an example (addapted from Example_Image2Pdf.au3) to load one or more images:

#include "MPDF_UDF.au3"

;set the properties for the pdf
_SetTitle("Image2PDF")
_SetSubject("Convert image(s) to pdf")
_SetKeywords("pdf, AutoIt")
_OpenAfter(True);open after generation
_SetUnit($PDF_UNIT_CM)
_SetPaperSize("a4")
_SetZoomMode($PDF_ZOOM_CUSTOM,90)
_SetOrientation($PDF_ORIENTATION_PORTRAIT)
_SetLayoutMode($PDF_LAYOUT_CONTINOUS)

;initialize the pdf
_InitPDF(@ScriptDir & "\Demo.pdf")

;=== load resources used in pdf ===
$var = FileOpenDialog("Select images", @ScriptDir & "\", "Images (*.jpg;*.bmp;*gif;*png;*tif)", 1 + 4 )
If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
 If IsArray($var) Then ; check if use choose one image or more
  $var = StringSplit($var, "|", 2)
  For $i=1 To UBound($var)-1
   _LoadResImage("taietel"&$i, $var[0]&"\"&$var[$i])
  Next
  ;load each image on it's own page
  For $i = 1 To UBound($var)-1
   _BeginPage()
    ;scale image to paper size!
    _InsertImage("taietel"&$i, 0, 0, _GetPageWidth()/_GetUnit(), _GetPageHeight()/_GetUnit())
   _EndPage()
  Next
 Else
  _LoadResImage("taietel", $var)
  _BeginPage()
  ;scale image to paper size!
  _InsertImage("taietel", 0, 0, _GetPageWidth()/_GetUnit(), _GetPageHeight()/_GetUnit())
  _EndPage()
 EndIf

;then, finally, write the buffer to disk
_ClosePDFFile()
EndIf

Test it with one image and then another test with multiple images. As I've said in the posted link above, the original example was intended for multiple images,

Edited by taietel

Share this post


Link to post
Share on other sites

Updated first post (v1.0.3).

happytc, I've fixed Example_Image2PDF.au3: the problem was with the $var (returned an array). Thanks. With this occasion I've rewrote also _LoadImageResource and made it smaller (got rid of the intermediary clone image in GDI+ stuff) and also the resulted pdf is smaller.

See if it works for you now.

Regards,

taietel

Share this post


Link to post
Share on other sites

happytc, see this:

Here is an example (addapted from Example_Image2Pdf.au3) to load one or more images:

#include "MPDF_UDF.au3"

;set the properties for the pdf
_SetTitle("Image2PDF")
_SetSubject("Convert image(s) to pdf")
_SetKeywords("pdf, AutoIt")
_OpenAfter(True);open after generation
_SetUnit($PDF_UNIT_CM)
_SetPaperSize("a4")
_SetZoomMode($PDF_ZOOM_CUSTOM,90)
_SetOrientation($PDF_ORIENTATION_PORTRAIT)
_SetLayoutMode($PDF_LAYOUT_CONTINOUS)

;initialize the pdf
_InitPDF(@ScriptDir & "\Demo.pdf")

;=== load resources used in pdf ===
$var = FileOpenDialog("Select images", @ScriptDir & "\", "Images (*.jpg;*.bmp;*gif;*png;*tif)", 1 + 4 )
If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
 If IsArray($var) Then ; check if use choose one image or more
  $var = StringSplit($var, "|", 2)
  For $i=1 To UBound($var)-1
   _LoadResImage("taietel"&$i, $var[0]&"\"&$var[$i])
  Next
  ;load each image on it's own page
  For $i = 1 To UBound($var)-1
   _BeginPage()
    ;scale image to paper size!
    _InsertImage("taietel"&$i, 0, 0, _GetPageWidth()/_GetUnit(), _GetPageHeight()/_GetUnit())
   _EndPage()
  Next
 Else
  _LoadResImage("taietel", $var)
  _BeginPage()
  ;scale image to paper size!
  _InsertImage("taietel", 0, 0, _GetPageWidth()/_GetUnit(), _GetPageHeight()/_GetUnit())
  _EndPage()
 EndIf

;then, finally, write the buffer to disk
_ClosePDFFile()
EndIf

Test it with one image and then another test with multiple images. As I've said in the posted link above, the original example was intended for multiple images,

I tested this example and the examples on Post#1(Version 1.0.3-modified today), 4 examples:

Example_Image2PDF.au3

Example_Mixed.au3

Example_Table.au3

Example_Txt2PDF.au3

Example_Table.au3 and Example_Txt2PDF.au3 worked

But Example_Image2PDF.au3 and Example_Mixed.au3 didn't work rightly

for open Example_mixed.pdf and Image2PDF.pdf by Foxit Reader:

My Foxit Reader said: format error:not a PDF or corrupted.

System: WinXP-SP3

System Language: Japanese

Share this post


Link to post
Share on other sites

runsnake, my testing environments are WinXP sp3 and Windows 7, both on 32 bit and language English.

Please, do the following: open the pdf with notepad and post the last lines, starting with 4 0 obj and finishing with %%EOF:

4 0 obj
<</ProcSet [/PDF/Text/ImageB/ImageC/ImageI]/XObject
.........................................................................................

%%EOF

Share this post


Link to post
Share on other sites

runsnake, my testing environments are WinXP sp3 and Windows 7, both on 32 bit and language English.

Please, do the following: open the pdf with notepad and post the last lines, starting with 4 0 obj and finishing with %%EOF:

4 0 obj
<</ProcSet [/PDF/Text/ImageB/ImageC/ImageI]/XObject
.........................................................................................

%%EOF

I downloaded the MPDFv103 from your post#1 just a moment ago, then tested the four examples. The result was the same as runsnake's:

Example_Image2PDF.au3 -> select image file: bmp.bmp, gif.gif, ico.ico

Example_Mixed.au3

Example_Table.au3

Example_Txt2PDF.au3

The two error pdf files here(there's no '4 0 obj' in the pdf):

my testing environments: Windows XP-sp3, 32bit, Chinese language

Share this post


Link to post
Share on other sites

happytc thank you for posting the results: I've just compared the Image2PDF results:

Posted Image

Here is the corresponding part of the icon image (16x16), on the left is the corrupted pdf and on the right is the working one; there are missing bites on the left (see the 6 0 obj compared to 10 0 obj on the right). The objects are different because we have selected images in different order, that's not important.

I think the code is altered because of the system language (Chinese or Japanese). Why? I really don't know! I have only tested on English systems because I use those.

I'll search for a solution.

Share this post


Link to post
Share on other sites

Taietel,

First, your udf is awesome.

I have an issue with it, though.

It generates a pdf that works fine on my 16 bit xp machine. But the same file has a problem on all 64 bit machines I have tried. They are windows 7 and windows 2008 r1 machines. Acrobat refuses to open the file and has this error:

"There was an error processing a page. There was a problem reading this document (135)."

If I remove all the autoit code from your sample that has to do with setting fonts and displaying text, it works fine, and only inserts images in the pdf. But if I even just restore a _LoadFontTT command, the acrobat error when trying to open the pdf is back.

So I looked at your _LoadFontTT function and found that there is a missing space just after "/LastChar". Adding the space makes it work.

__ToBuffer("<< /Type/Font/Subtype/TrueType/Name/" & $sAlias & "/BaseFont/" & $BaseFont & $sOptions & "/FirstChar " & $FirstChar & "/LastChar" etc

should be

__ToBuffer("<< /Type/Font/Subtype/TrueType/Name/" & $sAlias & "/BaseFont/" & $BaseFont & $sOptions & "/FirstChar " & $FirstChar & "/LastChar " etc

otherwise the buffer you generate will have "/LastChar255" instead of "/LastChar 255"

With this modification, I am able to generate a pdf with text that works on my 64 bit machines.

Keep up the good work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...