Results 1 to 7 of 7

Thread: Creating PDF file from ThinBASIC

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Lightbulb Creating PDF file from ThinBASIC

    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
    Attached Files Attached Files
    Last edited by Petr Schreiber; 30-08-2020 at 14:55.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    Member
    Join Date
    Nov 2012
    Location
    Missouri, USA
    Posts
    113
    Rep Power
    29
    Petr,

    I cannot find the fileformat_pdf.tBasicU file.

    Bill

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    Member
    Join Date
    Nov 2012
    Location
    Missouri, USA
    Posts
    113
    Rep Power
    29
    Petr,

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

    Bill

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Wow Petr, what a great code.

    Thanks a lot.
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Updated the first post to make it work in 2020 Please re-download the PDF.zip.


    Petr
    Last edited by Petr Schreiber; 30-08-2020 at 14:58.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Creating thinBasic keywords lists
    By ErosOlmi in forum Tips and Tricks
    Replies: 13
    Last Post: 29-12-2012, 03:45
  2. Chapter 2, example 4: Creating a program from file
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 3
    Last Post: 02-10-2012, 02:08
  3. Creating .lib file from a Dll
    By MikeTrader in forum 3rd party tools
    Replies: 3
    Last Post: 22-10-2009, 12:57
  4. Replies: 12
    Last Post: 13-06-2009, 22:00
  5. Creating .exe from thinbasic scripts
    By Macros The Black in forum thinBundle
    Replies: 1
    Last Post: 02-06-2009, 09:36

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •