PDA

View Full Version : Creating PDF file from ThinBASIC



Petr Schreiber
30-04-2013, 10:27
If you could use some PDF creation from script, please read on :)

I converted the original José Roca's Haru Free PDF Library 2.1.0 headers to ThinBASIC. As the library requires callback for code handling I created high level wrapper, which makes the PDF creation a bit easier. Here sample code:


'
' Test of PDF wrapper
'

Uses "Console", "Math"

#INCLUDE "fileformat_pdf.tBasicU"

Function TBMain()

' -- Initialize library
pdf_InitLibrary()

' -- Create document
String sPDFFile = APP_SourcePath + "Test.pdf"
tPdfDocument hDocument = pdf_CreateDocument()

' -- Font used
String fArialName = pdf_GetFontNameFromTTF(hDocument, "C:\Windows\Fonts\arial.ttf", TRUE)
tPdfFont fArial = pdf_CreateFont(hDocument, fArialName , "CP1250")

' -- Create page and retrieve the dimensions
tPdfPage hPage
tPdfNumber nHeight, nWidth

hPage = pdf_AddPage(hDocument)
pdf_GetPageSize(hPage, nWidth, nHeight)

' -- Create content - the coordinates are as in math:
' -- 0,0 is lower left corner

' -- Draw rectangle, with different contour and content
pdf_SetStrokeColor(hPage, Rgb(96,96,96))
pdf_SetFillColor(hPage, Rgb(224,224,224))
pdf_DrawRectangle(hPage, 10, nHeight-10, nWidth-20, nHeight-20)

' -- Draw cross inside it, using 3px orange line
pdf_SetStrokeColor(hPage, Rgb(255,128,64))
pdf_SetLineWidth(hPage, 3)
pdf_DrawLine(hPage, 10, 10, nWidth-10, nHeight-10)
pdf_DrawLine(hPage, 10, nHeight-10, nWidth-10, 10)

' -- Write text
Long i
pdf_SetFillColor(hPage, Rgb(0, 0, 0))
For i = 1 To 8
pdf_SetFont (hPage, fArial, 10+i*2)
pdf_PrintText(hPage, 225, nHeight-20-i*(10+i*2), "Hello ThinBASIC!")
Next

' -- Draw polygon
Long radius

For radius = 160 To 10 Step - 10
pdf_SetColor(hPage, Rgb(Rnd(128,255), Rnd(128,255), Rnd(128,255)))
pdf_BeginPoly(hPage, nWidth/2, nHeight/2)

For i = 0 To 360 Step 60
pdf_AddPoly(hPage, nWidth/2+Cos(DegToRad(i))*radius, nHeight/2+Sin(DegToRad(i))*radius)
Next

pdf_EndPoly(hPage, TRUE)
Next


' -- Draw image
pdf_DrawImage(hDocument, hPage, nWidth/2-64, nHeight/2-256, APP_SourcePath+"picture.png", 128, 128 )

' -- Save to disk
pdf_SaveDocument(hDocument, sPDFFile)

PrintL
PrintL "File saved as:"
PrintL sPDFFile
PrintL
PrintL "Press any key to quit..."
WaitKey
' -- Release document from memory
pdf_ReleaseDocument(hDocument)

' -- Release library
pdf_ReleaseLibrary()

End Function


You can download the converted headers + my special wrapper + sample code from the attachement of this post. I hope you will find use for it.

The wrapper offers these functions, each is documented in the fileFormat_PDF.tBasicU file:

pdf_InitLibrary, pdf_ReleaseLibrary
pdf_CreateDocument, pdf_SaveDocument, pdf_ReleaseDocument
pdf_AddPage, pdf_GetPageSize
pdf_GetFontNameFromTTF, pdf_CreateFont, pdf_SetFont, pdf_PrintText
pdf_SetStrokeColor, pdf_SetFillColor, pdf_SetColor
pdf_SetLineWidth, pdf_SetLineStyle
pdf_DrawRectangle, pdf_DrawLine, pdf_BeginPoly, pdf_AddPoly, pdf_EndPoly, pdf_DrawImage


Of course, the original headers from José offer even more power.


Petr

Billbo
30-04-2013, 15:09
Petr,

I cannot find the fileformat_pdf.tBasicU file.

Bill

Petr Schreiber
30-04-2013, 15:32
Hi Bilbo,

please download the PDF.ZIP from the first post, it contains:

DemonstrationOfPDFCreation.tBasic (sample script)
fileformat_PDF.tBasicU (my wrapper)
hpdf (header)
hpdf_consts (header)
hpdf_types (hpdf_types)
libhpdf.dll
libpng13.dll
picture.png (used by example)


Petr

Billbo
30-04-2013, 15:44
Petr,

Thanks. I was in a hurry and scanned the files in the zip
to fast.

Bill

ErosOlmi
30-04-2013, 22:26
Wow Petr, what a great code.

Thanks a lot.
Eros

Petr Schreiber
01-05-2013, 12:17
Thanks, but the biggest work is done by the authors of the original library. Some of my functions are just 1:1 wrappers.
But I added even some other higher level abstractions to make the usage of the library more simple - and easy to read, in BASIC tradition :)


Petr

Petr Schreiber
30-08-2020, 14:56
Updated the first post to make it work in 2020 :) Please re-download the PDF.zip.


Petr