Page 1 of 9

HMG_HPDF - Export to PDF made easy...

Posted: Fri Sep 28, 2012 4:14 pm
by Rathinagiri
Dear friends,

I have tried to implement similar commands to HMG Print system using official HaruPDF library.

Some points to note:
- I could not implement 'Rounded' rectangle alone. :) All other commands are implemented.

- Font Bold, Italics, underline and strikeout are also not implemented as they are not available in this library.

- TTF fonts can also be used if you provide the exact path to the font file (like "c:\windows\fonts\myfont.ttf") or copy the font to the application folder and provide ("myfont.ttf"). The TTF font will be embedded into the PDF file automatically.

- Only image files with png extension can be drawn.

This is the initial version. Please give your valuable suggestions for improvement.

Now the sample application code:

Code: Select all

#include <hmg.ch>
#include "hmg_hpdf.ch"

Function Main

   define window main at 0, 0 width 300 height 300 main title 'HMG HPDF Sample'
      define button print
         row 100
         col 100
         caption 'Create PDF'
         action pdf_create()
      end button
   end window
   main.center
   main.activate

Return

function pdf_create
   local lSuccess := .f.
   SELECT HPDFDOC 'sample.pdf' TO lSuccess papersize HPDF_PAPER_A4
   if lSuccess
      START HPDFDOC
         START HPDFPAGE
         @ 10, 60 HPDFPRINT 'This is sample Left Aligned' 
         @ 15, 60 HPDFPRINT 'This is sample Red Colored' color { 255, 0, 0 }
         @ 20, 60 HPDFPRINT 'This is sample Center Aligned' CENTER
         @ 25, 60 HPDFPRINT 'This is sample Right Aligned' RIGHT
         @ 30, 10 HPDFPRINT 'This is a small paragraph to be printed inside a rectangular area which is left aligned.' to 50, 60 
         @ 60, 10 HPDFPRINT 'This is a small paragraph to be printed inside a rectangular area which is center aligned.' to 80, 60 CENTER
         @ 90, 10 HPDFPRINT 'This is a small paragraph to be printed inside a rectangular area which is right aligned.' to 110, 60 RIGHT
         @ 120, 10 HPDFPRINT 'This is a small paragraph to be printed inside a rectangular area which is justify aligned.' to 140, 60 JUSTIFY
         @ 150, 10 HPDFPRINT line to 160, 10
         @ 150, 20 HPDFPRINT line to 160, 20 penwidth 0.5
         @ 150, 30 HPDFPRINT line to 160, 30 penwidth 1 color { 0, 255, 0 }
         END HPDFPAGE
         START HPDFPAGE
         @ 10, 60 HPDFPRINT 'This is Page 2' 
         @ 30, 20 HPDFPRINT rectangle to 60, 50
         @ 40, 30 HPDFPRINT rectangle to 50, 40 penwidth 1 color { 0, 255, 0 }
         @ 70, 20 HPDFPRINT rectangle to 90, 40 penwidth 1 color { 0, 255, 0 } filled
         @ 100, 10 HPDFPRINT 'This is text in BIG size' size 30
         @ 120, 10 HPDFPRINT 'This is in Times-Roman font' font 'Times-Roman' size 18
         @ 140, 10 HPDFPRINT 'This is in Courier-BoldOblique font' font 'Courier-BoldOblique' size 18
         @ 160, 10 HPDFPRINT 'This is in Times-Italic font' font 'Times-Italic' size 18
         @ 180, 10 HPDFPRINT 'This is in Helvetica-Bold font' font 'Helvetica-Bold' size 18
         @ 200, 10 HPDFPRINT 'This is in Courier font' font 'Courier' size 18
         END HPDFPAGE
         START HPDFPAGE
         @ 10, 60 HPDFPRINT 'This is Page 3' 
         @ 20, 10 HPDFPRINT IMAGE 'peacock.png' width 140 height 106
         END HPDFPAGE
      END HPDFDOC
   endif   
   execute file 'sample.pdf'      
return nil
pdf.zip
(263.94 KiB) Downloaded 346 times

Re: HMG_HPDF - Export to PDF made easy...

Posted: Fri Sep 28, 2012 4:32 pm
by esgici
Thanks a lot Rathi :)

Definitely important improvement in HMG :arrow:

I'll check and will present my impressions as soon as possible.

TIA

Best regards

Re: HMG_HPDF - Export to PDF made easy...

Posted: Fri Sep 28, 2012 5:20 pm
by esgici
Hi Rathi

Examined, everything is OK :D

Thanks again :)

Best regards

Re: HMG_HPDF - Export to PDF made easy...

Posted: Fri Sep 28, 2012 10:01 pm
by esgici
I want try to convert and test SAMPLES\PRINT\ImageFromFile\jpg\demo.prg ( after converted .jpg to .png )

Sadly unsuccessful :(
Unrecoverable error 6005: Exception error:

Exception Code:C0000005
Exception Address:004369F0
EAX:00003120 EBX:00000000 ECX:00000001 EDX:00000000
ESI:00575DD4 EDI:0056B1C0 EBP:007B7FA4
CS:EIP:001B:004369F0 SS:ESP:0023:0022F1C8
DS:0023 ES:0023 FS:003B GS:0000
Flags:00010246
CS:EIP: 8B 43 0C 89 44 24 30 83 C4 28 5B E9 B0 A8 00 00
SS:ESP: 007927A0 00001051 007B7FA4 0049A5D6 00000019 007927A0 007C1EB8 0048E36E 00000019 0022F254 0022F254 0042CDAF 00000000 4414D1A3 007BA684 00000002

C stack:
EIP: EBP: Frame: OldEBP, RetAddr, Params...
004369F0 007B7FA4 007C1EC8 007C20AC 007C1D3C 007C1EB8 00000000 00000000 00000000 00000000 00000000 00000000
And I din't find where I'm wrong :(

Best regards
HPDFDemo.zip
Files of erroneous HPDFDemo project
(38.63 KiB) Downloaded 222 times

Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 4:32 am
by Rathinagiri
Hi Esgici,

The culprit is font. I will write in detail how to use the fonts. Just to check please use this code:

Code: Select all

#include <hmg.ch>
#include "hmg_hpdf.ch"

Function Main

	Private aColor [10]

	aColor [1] := YELLOW	
	aColor [2] := PINK	
	aColor [3] := RED	
	aColor [4] := FUCHSIA	
	aColor [5] := BROWN	
	aColor [6] := ORANGE	
	aColor [7] := GREEN	
	aColor [8] := PURPLE	
	aColor [9] := BLACK	
	aColor [10] := BLUE
   
   define window main at 0, 0 width 300 height 300 main title 'HMG HPDF Sample'
      define button print
         row 100
         col 100
         caption 'Print'
         action PrntToPdf()
      end button
   end window
   main.center
   main.activate

Return

*------------------------------------------------------------------------------*

PROCEDURE PrntToPdf()

   LOCAL nPageNo 

   SELECT HPDFDOC 'sample.pdf' TO lSuccess papersize HPDF_PAPER_A4
   
   IF lSuccess
      START HPDFDOC
      
   		FOR nPageNo := 1 TO 10
         
            START HPDFPAGE

               @ 20,20 HPDFPRINT RECTANGLE ;
                  TO 50,190 ;
                  PENWIDTH 0.1
                  
               @ 25,25 HPDFPRINT IMAGE "hmg.png" ;
                  WIDTH 20 ;
                  HEIGHT 20 

               @ 30,85 HPDFPRINT "HPDFPRINT DEMO" ;
                  SIZE 24 COLOR aColor[ nPageNo ]
               
               @ 140,60 HPDFPRINT "Page Number :" + Str( nPageNo, 2 ) ;
                  SIZE 20 COLOR aColor [nPageNo]
               
               @ 260,20 HPDFPRINT LINE ;
                  TO 260,190 ;
                  PENWIDTH 0.1
               
			   END HPDFPAGE

         NEXT nPageNo

	   END HPDFDOC

      EXECUTE FILE 'sample.pdf'      
      
   ELSE
   
      MsgStop( "Couldn't SELECT HPDFDOC", "ERROR !" )
      
   ENDIF lSuccess
   
RETURN 


Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 4:34 am
by Rathinagiri
Now I have fixed some bugs and improved the code. Please check...
pdf.zip
(263.94 KiB) Downloaded 240 times

Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 4:41 am
by Rathinagiri
Now, some words about using fonts in this library from the original site.

There are several types of fonts available in libHaru.

1. Base14 Font - The built-in font of PDF. Can be used for all viewer applications.
2. Type1 Font - A font format used by PostScript.
3. TrueType Font - Widely used outline font format.
4. CID Font - Font format for multi-byte characters. Developed by Adobe.

1. Base14 Fonts:

Base14 fonts are built into PDF and all viewer applications can display these fonts. An application can get a base14 font handle any time. PDF files which use base14 fonts are smaller than those which use other type of fonts. Moreover, PDF processing is faster because there is no need to load external fonts. However, base14 fonts are only able to display the latin1 character set. To use other character sets, an application must use other fonts.

Built-in base14 fonts

Courier
Courier-Bold
Courier-Oblique
Courier-BoldOblique
Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique
Times-Roman
Times-Bold
Times-Italic
Times-BoldItalic
Symbol
ZapfDingbats

2. Type1 Fonts

Type1 is a format of outline fonts developed by Adobe. An AFM file is necessary to use an external Type1 font on Haru. If a PFA/PFB file is specified at invoking, the glyf data of the font is embedded to the PDF file. Otherwise, only metrics data in AFM file is embedded.

3. TrueType Fonts

libHaru can use TrueType fonts. There are two types of TrueType fonts. The first format, with ".ttf" extension, contains only one font-data in the file. The second format, with ".ttc" extension, contains multiple font-data in the file. You have to give the exact location of the ttf file to include. (for example c:\windows\fonts\arial.ttf)

Haru can use only TrueType fonts which have Unicode cmap and the following tables. "OS/2", "cmap", "cvt ", "fpgm", "glyf", "head", "hhea", "hmtx", "loca", "maxp", "name", "post", "prep".

4. CID Fonts

CIDFont is a font for multi byte character developed by Adobe. Two simplified Chinese fonts, one traditional Chinese fonts, four Japanese fonts, and four Korean fonts are available on Haru. An application have to invoke the following functions once before using CID fonts.

For more information, you can read here:

http://libharu.org/wiki/Documentation/Fonts

Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 7:54 am
by Rathinagiri
I have found some exclusive points regarding PDF.

1. You can have 200 inches by 200 inches paper definition. :)

2. The limitation of total number of pages in a PDF is 8191. More than that, we have to call some other functions in the library.

Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 9:04 am
by bpd2000
Dear rathinagiri

Excellant work, Include sample program in next HMG version release

Re: HMG_HPDF - Export to PDF made easy...

Posted: Sat Sep 29, 2012 10:54 am
by esgici
Hi Rathi
rathinagiri wrote:The culprit is font...
some words about using fonts in this library...
Thanks to clear clarification :D

Do you think now an example for tabular format output?

Best regards