[Module] pbPDF-Module

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Jan2004
Enthusiast
Enthusiast
Posts: 156
Joined: Fri Jan 07, 2005 7:17 pm

Re: [Module] pbPDF-Module

Post by Jan2004 »

Thanks to normeus and Little_man for directing me, and many thanks to Thorsten1867 for creating such a great PDF library.
All varieties of the Lato fonts have Polish characters. I made a mistake in the previous post because I marked the font access incorrectly. It is best to place fonts in the same directory as pbPDFModule.pbi, and place my example where the example files are located, in the Examples directory. All fonts Lato also covers the Russian language (I studied Russian in secondary and high school) and the language of my wonderful Slovak neighbors (I live 20 km from the border).

In my opinion, the Lato font is probably the best font for content created in PDF. Lato can be included in commercial applications freely.

Code: Select all

XIncludeFile "..\pbPDFModule.pbi"

Define File$="multilingual_lato_fonts.pdf"
#PDF = 1
;  Create an instance of PDF
If PDF::Create(#PDF)
  ;  Always add a page to create a PDF or you'll get an error
  ;  Page size and orientation is optional, I like to add it because you can have multiple page sizes in a PDF   
  PDF::AddPage(#PDF,PDF::#Landscape,PDF::#format_letter)
  
  ;All Lato fonts has polish, russian, slovakian letters
  ; Lato-Italic.ttf
  ; Lato-Light.ttf
  ; Lato-LightItalic.ttf
  ; Lato-Medium.ttf
  ; Lato-MediumItalic.ttf
  ; Lato-Semibold.ttf
  ; Lato-SemiboldItalic.ttf
  ; Lato-Thin.ttf
  ; Lato-HeavyItalic.ttf
  ; Lato-ThinItalic.ttf
  ; Lato-Regular.ttf
  ; Lato-Black.ttf
  ; Lato-BlackItalic.ttf
  ; Lato-Bold.ttf
  ; Lato-BoldItalic.ttf
  ; Lato-Hairline.ttf
  ; Lato-HairlineItalic.ttf
  ; Lato-Heavy.ttf
  
  PDF::EMbedFont(#PDF, "..\Lato-Light.ttF","Lato","",PDF::#Unicode)
  PDF::SetFont(#PDF, "Lato", "", 13)
  PDF::PlaceText(#PDF, "In polish: Zażółć gęślą jaźń.", 10, 40)
  PDF::PlaceText(#PDF, "What means: Yellow the goose self.", 10, 45)
  
  PDF::EMbedFont(#PDF, "..\Lato-Semibold.ttf","Lato2","",PDF::#Unicode)
  PDF::SetFont(#PDF, "Lato2", "", 12)   
  PDF::PlaceText(#PDF, "In slovac: Orly lietajú najvyššie a prečo sú také chytré.", 10, 60)
  PDF::PlaceText(#PDF, "What means: Eagles fly the highest and why they are so smart.", 10, 65)
  
  PDF::EMbedFont(#PDF, "..\Lato-BoldItalic.ttf","Lato3","",PDF::#Unicode)
  PDF::SetFont(#PDF, "Lato3", "", 11)
  PDF::PlaceText(#PDF, "In russian: Пусть всегда будет солнце, Пусть всегда будет небо, Пусть всегда будет мама, Пусть всегда буду я. ", 10, 80)
  PDF::PlaceText(#PDF, "What means: May there always be the sun, May there always be the sky, May there always be my mum, May there always be me.", 10, 85)
  PDF::Close(#PDF, File$)
  
EndIf

RunProgram(File$)
matlab2
New User
New User
Posts: 2
Joined: Fri Aug 27, 2021 1:20 pm

Re: [Module] pbPDF-Module

Post by matlab2 »

Dear all, I need your help again. With the great PDF module from Thorsten1867, I create serial letters/reports in many languages using templates. It works very well. When generating Ukrainian letters using Unicode fonts with Cyrillic characters (Lato Font familie or Paratype PT Sans), there are two glyphs [Ш Щ] - and only the two - that cause the PDF generation to be cancelled depending on the "environment". I have added examples to the code from Jan2004 (see above) so that you can see the strange behaviour. I would be very, very happy to receive ideas for a solution.

Code: Select all

; The example is based on code from Jan2004 (see above).

XIncludeFile "pbPDFModule.pbi"

Define File$="pbPDF-Example.pdf"
#PDF = 1
If PDF::Create(#PDF)
 
  PDF::AddPage(#PDF,PDF::#Landscape,PDF::#format_Letter)
  
  ; specialty unicode font, which contains the character you need  
  PDF::EMbedFont(#PDF, "Lato-Regular.ttf","Lato","",PDF::#Unicode) 
  PDF::SetFont(#PDF, "lato", "", 14)
  
  ; Here are a few examples with different outcomes
  #test = 1 ; 1-6
  
  Select #test
    Case 1
      ; works  
      PDF::PlaceText(#PDF, "Ш Щ", 10, 50)
    Case 2
      ; does not work  
      PDF::PlaceText(#PDF, "Ш", 10, 50)
    Case 3
      ; does not work 
      PDF::PlaceText(#PDF, "Щ", 10, 50)
    Case 4
      ; works
      PDF::PlaceText(#PDF, "Ц", 10, 50) 
    Case 5
      ; works in parts
      PDF::PlaceText(#PDF, "Ш Щ", 10, 50)
      PDF::PlaceText(#PDF, "Ш", 10, 70)
      PDF::PlaceText(#PDF, "Щ", 10, 90)
    Case 6
      ; works
      PDF::PlaceText(#PDF, "ЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяѐёђѓєѕіїјљњћќѝўџѠѡѢѣѤѥѦѧѨѩѪѫѬѭѮѯѰѱѲѳѴѵѶѷѸѹѺѻѼѽѾѿҀҁ҂$", 10, 50) 
  EndSelect

  PDF::Close(#PDF, File$)
  
EndIf

RunProgram(File$)
Jan2004
Enthusiast
Enthusiast
Posts: 156
Joined: Fri Jan 07, 2005 7:17 pm

Re: [Module] pbPDF-Module

Post by Jan2004 »

Try using a different font, for example from here:
https://russianfonts.org
Jan2004
Enthusiast
Enthusiast
Posts: 156
Joined: Fri Jan 07, 2005 7:17 pm

Re: [Module] pbPDF-Module

Post by Jan2004 »

Dear Colleagues, could you show me the working code for inserting a PNG image into PDF using the ImageMemory function? The code on page 4 of this post does not work.
Thank you for your help. Kind regards.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: [Module] pbPDF-Module

Post by Paul »

Jan2004 wrote: Sat Apr 06, 2024 9:15 am Dear Colleagues, could you show me the working code for inserting a PNG image into PDF using the ImageMemory function? The code on page 4 of this post does not work.
Thank you for your help. Kind regards.
You made it to page 4... you just had to go 2 more pages :)
https://www.purebasic.fr/english/viewto ... 36#p570236
Image Image
Jan2004
Enthusiast
Enthusiast
Posts: 156
Joined: Fri Jan 07, 2005 7:17 pm

Re: [Module] pbPDF-Module

Post by Jan2004 »

Paul,
I was already wondering about the code you indicated (the one on page 6). This code also did not meet my expectations. Why? Because after compiling this code a PNG file outside the compiled code still will be needed. And I wanted to have a PNG file packaged in an EXE. However, I thank you for the inspiration, because I started thinking about this file again and added a few lines to the code you indicated. Now I have what I longed for.

Code: Select all

IncludeFile "..\pbPDFModule.pbi"

Define LinkID.i, File$ = "pbPDF_image_packed_in_PDF.pdf"
Define *pdf_1_mem,pdf_1_img,pdf_1_size
#PDF = 1

If PDF::Create(#PDF)
  
  PDF::AddPage(#PDF)
  
  LinkID = PDF::AddLinkURL(#PDF, "https://www.purebasic.com/")
  Debug LinkID
  
  ; "DataSection" and "CatchImage" will cause the PNG to be loaded into the EXE file.
  DataSection 
  clipart:
    IncludeBinary"MYFILE.PNG"
  EndDataSection 
  
  pdf_1_img = CatchImage(#PB_Any,?clipart)
  
  If pdf_1_img                                  ; if there was an image in that location
    *pdf_1_mem=EncodeImage(pdf_1_img,#PB_ImagePlugin_PNG);
    If *pdf_1_mem
      pdf_1_size= MemorySize(*pdf_1_mem)
      ;pdf::ImageMemory(ID.i, ImageName.s, *Memory, Size.i, Format.i, X.f=#PB_Default, Y.f=#PB_Default, Width.f=#PB_Default, Height.f=#PB_Default, Link.i=#NoLink)
      pdf::imagememory(#pdf,"MYFILE.PNG",*pdf_1_mem,pdf_1_size,pdf::#Image_PNG,20,20,60,20,LinkID)
    EndIf     
    FreeMemory(*pdf_1_mem)
    FreeImage(pdf_1_img)
  EndIf
  PDF::Close(#PDF, File$)
EndIf

RunProgram(File$)
Do you Paul and other colleagues have any additional comments on the current version of the PB file?
To check whether the concept is working correctly:
1. Create your own MYFILE.PNG file and know its dimensions.
2. Enter the MYFILE.PNG data into the code.
3. Create an EXE file from the code using the PureBasic "Compiler/Create Executable" function.
4. Rename the MYFILE.PNG file to, for example, MYFILE_XX.PNG.
5. Execute the EXE file created from the PB code.
If the image is visible in PDF, it means that we have achieved our intended goal.

Many thanks to Thorsten1867 for creating a wonderful PDF library, which works great in the PureBasic 5.73 version I use.
Post Reply