Creating a PDF from text variable (command line) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Creating a PDF from text variable (command line)

Post by ahk7 » 11 Apr 2021, 02:34

btw you can also generate barcodes directly using AutHotkey https://www.autohotkey.com/boards/viewtopic.php?t=5538

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Creating a PDF from text variable (command line)

Post by labrint » 14 Apr 2021, 03:29

ahk7 wrote:
10 Apr 2021, 16:40
Just quickly modified an example, this prints the text on an A4 page with the Euro sign for me.

Code: Select all

#Include hpdf.ahk

page_title := "SAN RAFFAEL PHARMACY"
page_desc := "VOLTAREN EMULGEL 1.0%"
page_price := "€1.00"
page_origin := "VJ SALOMONE 03032021 VAT: 18% INV: 416,422"

Text2Pdf(page_title "`n" page_desc "`n" page_price "`n" page_origin ,"testeuro.pdf")

Text2Pdf(text,target="")
{
   remaining := textLength := StrLen(text)

   if targetDir =
      targetDir = .

   if target =
      target := targetDir "\" noExt ".pdf"

   ;load the library
   hpdfDllFile := A_ScriptDir "\libhpdf.dll"
   hDll := HPDF_LoadDLL(hpdfDllFile)

   ;create new document in memory
   hDoc := HPDF_New("error","000")

   ;Load a Standard-font
   encoding := "WinAnsiEncoding"
   fontName := HPDF_LoadTTFontFromFile(hDoc, A_WinDir "\Fonts\arial.ttf", 0)
   font := HPDF_GetFont2(hDoc, fontName, encoding)
   MAX_TEXT_POST := 25000

   ;if there are characters left to print, write them to a new page
   while remaining > 0
   {
      ;Creating a page, A4, Landscape
      ;+get dimensions for further usage
      page := HPDF_AddPage(hDoc)
      HPDF_Page_SetSize(page, "A4", "PORTRAIT")
      height := HPDF_Page_GetHeight(page)
      width := HPDF_Page_GetWidth(page)

      ;Writing the header
      HPDF_Page_BeginText(page)
      HPDF_Page_SetFontAndSize(page, font, 11)
      header := fileNameNoPath " (Page " A_Index ")"
      tw := HPDF_Page_TextWidth(page, header)
      HPDF_Page_MoveTextPos(page, (width-tw)/2, height-20)

      HPDF_Page_ShowText(page, header)
      HPDF_Page_EndText(page)


      ;Write the text to the page
      HPDF_Page_BeginText(page)
      HPDF_Page_SetFontAndSize(page, font, 11)

      ;text-dimensions on the page
      rect_left := 20
      rect_top := height-40
      rect_right := width-40
      rect_bottom := 20

      HPDF_Page_TextRect(page, rect_left, rect_top, rect_right, rect_bottom, SubStr(text,textLength-remaining+1,MAX_TEXT_POST), "LEFT", charsWritten)
      remaining := remaining - charsWritten

      HPDF_Page_EndText(page)
   }

   ;saving to file
   HPDF_SaveToFile(hDoc, target)

   ;unload dll
   HPDF_UnloadDLL(hDll)
}   
HPDF_Page_TextRect() was key to solving the character issue

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Creating a PDF from text variable (command line)

Post by labrint » 12 Mar 2024, 09:38

Guys, can anyone produce a script that can print out these characters Aa Bb Ċċ Dd Ee Ff Ġġ Gg Għgħ Hh Ħħ Ii Ieie Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Zz Żż (Maltese character set) on a pdf using libharu?

I'm having trouble printing anything other than the English alphabet. I already use libharu wrapper and dlls.

Code: Select all

	font := HPDF_GetFont(hDoc, "Helvetica-Bold", 0)
	HPDF_Page_SetFontAndSize(page, font, 8)  ; Convert font size from mm to points


	text := "this is a test"

	HPDF_Page_BeginText(page)
	HPDF_Page_MoveTextPos(page, leftMargin, pageHeight - topMargin - mmToPoints(3))  ; Adjust Y position for font size, converted to points
	HPDF_Page_ShowText(page, text)
	HPDF_Page_EndText(page)
I want to be able to print the text also if it has the Maltese Characters above.

I tried loading a different font from the windows font folder such as Arial and encoding it to iso-8859-3
https://www.marxists.org/admin/charsets/iso-8859-3.htm

I tried encoding my ahk text file to UTF 8 and UTF 8 bom yet no success.
I tried converting the text from ansi to utf and then processing the utf string.
Spent a lot of hours trying to do this to no success.

Please help

Post Reply

Return to “Ask for Help (v1)”