[Module] pbPDF-Module

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: [Module] pbPDF-Module

Post by StarBootics »

Kwai chang caine wrote: Sat May 29, 2021 6:31 pm Hello at all
Excuse by advance this stupid question :oops: , but is it possible to simply open and show an existing PDF ? with this splendid module 8)
I'm pretty sure that this module does not contain a PDF document renderer. It can be used to create a PDF document that can be opened with a PDF document reader.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Module] pbPDF-Module

Post by Kwai chang caine »

Thanks for your answer StarBootics, this is what i thought too, but i still wanted to be sure :wink:
I'm looking for a code that can open an existing PDF and modify it
In any case, Thorsten1867 really did a big job, like always :shock: 8)
ImageThe happiness is a road...
Not a destination
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: [Module] pbPDF-Module

Post by StarBootics »

Kwai chang caine wrote: Mon May 31, 2021 7:25 pm I'm looking for a code that can open an existing PDF and modify it
I think you will have to create it your self.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

I have been using pbPDF without any problems up through PureBasic 5.73 Win, both 32 and 64-bit versions. However, I it crashes when I try to use it in the trial Alpha3 version of PB6.00. PbPDF compiles ok, and the program runs, but generates an Invalid Memory Access error for the line
Version = ipdf_EndianL(*TTF_Memory\sfnt_version) when it reaches the line.

Norm suggested I try a workaround by DoubleDutch for the endian functions but the compiler does not seem to recognize the constant #PB_Processor_C. Won't compile. I have tried using both the asm and c-backend compilers with the same results.
The line is CompilerIf #PB_Compiler_Processor=#PB_Processor_C

Any ideas?
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

Sorry, I found Fred's statement on the constant. It is now : #PB_Compiler_Backend, #PB_Backend_Asm and #PB_Backend_C. Testing for that works, but the endian functions still crash when compiled with the asm instead of C backend.
User avatar
HeX0R
Addict
Addict
Posts: 979
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: [Module] pbPDF-Module

Post by HeX0R »

I can't find that ipdf_EndianL() anywhere in the pbPDFModule.pbi, where is that coming from?
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: [Module] pbPDF-Module

Post by Lord »

@DeanH & @HeX0R:

I'm using PurePDF2.0 distributed ba Normeus: http://normeus.com/purepdf/

I had to include a change suggested by DoubleDutch: viewtopic.php?f=4&t=77331&p=570608
At least the ASM back end worked with pdf:

Code: Select all

CompilerIf #PB_Backend_C; in Alpha 3
	
	Procedure.l ipdf_EndianL(value.l)
		ProcedureReturn (value>>24&$ff)|(value>>8&$ff00)|(value<<24&$ff000000)|(value<<8&$ff0000)
	EndProcedure
	
	Procedure.q ipdf_EndianQ(value.q)
		ProcedureReturn	(value>>56&$ff)|(value>>40&$ff00)|(value>>24&$ff0000)|(value>>8&$ff000000)|(value<<56&$ff00000000000000)|(value<<40&$ff000000000000)|(value<<24&$ff0000000000)|(value<<8&$ff00000000)
	EndProcedure
	
	Procedure.w ipdf_EndianW(value.w)
		ProcedureReturn (value>>8&$ff)|(value<<8&$ff00)
	EndProcedure
	
CompilerElse
	
	;Thanks to Rescator / skywalk / wilbert for the Procedures EndianW() / EndianL() / EndianQ()
	Procedure.l ipdf_EndianL(value.l)
	  ; Rescator : http://forums.purebasic.com/english/viewtopic.php?p=84270&sid=7f3f06eae02ad44b303655fb722bb0f0#p84270
	  EnableASM
	  MOV Eax,value
	  BSWAP Eax
	  DisableASM
	  ProcedureReturn
	EndProcedure
	
	CompilerIf #PB_Compiler_Processor=#PB_Processor_x64
	  Procedure.q ipdf_EndianQ(value.q)
	    ; Rescator : http://www.purebasic.fr/english/viewtopic.php?p=84270#p84270
	    EnableASM
	    MOV rax, value
	    BSWAP rax
	    DisableASM
	    ProcedureReturn
	  EndProcedure
	CompilerElse
	  Procedure.q ipdf_EndianQ(value.q)
	    ; Wilbert : http://www.purebasic.fr/english/viewtopic.php?p=361932#p361932
	    Protected addr.l=@value
	    EnableASM
	    MOV edx, addr
	    MOV eax, [edx + 4]
	    MOV edx, [edx]
	    BSWAP eax
	    BSWAP edx
	    DisableASM
	    ProcedureReturn
	  EndProcedure
	CompilerEndIf
	
	Procedure.w ipdf_EndianW(value.w)
	  ; skywalk/wilbert : http://forums.purebasic.com/english/viewtopic.php?p=352259&sid=7f3f06eae02ad44b303655fb722bb0f0#p352259
	  EnableASM
	  ROL value, 8
	  DisableASM
	  ProcedureReturn value
	EndProcedure

CompilerEndIf
I do not use C backend anymore because there are a lot of troubles and I'm waiting for a new version of PB.
Image
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: [Module] pbPDF-Module

Post by normeus »

Lord,
Thank you for the changes, I will add them to the file I am hosting.

Everyone,
pbPDF-Module ( link at the beginning of this thread ) is better. it has been updated by Thorsten1867.
I searched pbPDF-Module for any sign of ASM and I couldn't find anything. tried some Examples and they worked using "PB 6 version 2 C backend" . No need to check for "CompilerIf #PB_Backend_C".
I don't use the module so I am not able to test it in a real life situation. If you have a small code sample I could test it.
[edit]
I downloaded version 3 and it also worked fine.
your test for #PB_backend_C should look more like this for it to work on pb 5.72 without getting an error about constant not found (only change the compilerif) :

Code: Select all

CompilerIf Defined(PB_Backend_C,#PB_Constant); in Alpha 3      <--- if constant  not defined you are running ASM from older PB
  Debug " Alpha 3"
CompilerElse
  Debug "Good old ASM here"
 CompilerEndIf
If you were getting a constant not found error then you were not running version 3

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

I finally got pbPDF to work in PB6. The problem I had was my fault. I was using PurePDF but did not realize it. The endian functions are not in pbPDF but they are in the older one. I thought I had changed before but discovered I hadn't. :oops: My apologies for the panic. pbPDF seems to compile Ok in both the asm and c backend modes.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

I have a problem involving fonts other than Helvetica, Courier or Times.

From what I've read, to use a font other than Arial or Times in pbPDF, the font needs to be embedded with the function EmbedFont before SetFont can be used. This function requires the font's filename as well as font family name. An example font would be Comic Sans MS.

Is there a way to use/set a font by its name without having to know the filename?
I have had a look at the source code and it is beyond me.
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: [Module] pbPDF-Module

Post by normeus »

DeanH,

You have to know the font filename to be able to load it into the pdf. You don't need the font family name or better put, font family name is currently not working.
it's easy to find the filename on windows:
Start->Settings->fonts then click on the font you want and it will give you the filename. Currently "Font Family Name" is the name pbPDF will use to access the font you loaded, so if you call the font "FUNNY" instead of "Comic" it will find and use "FUNNY".
Only Underline works with loaded fonts. Not Bold or Italic.

Code: Select all

XIncludeFile "..\pbPDFModule.pbi"

Define File$="pbPDF-EmbedFont-1.pdf"
;
;On windows, From start button click on settings cog then click on fonts. to the right click on the font you want
; it will give you the "Font file name"
;
; currently EmbedFont works this way {filename of font}{name to identify font}{style (not working)}{UNICODE FLAG}
;

#PDF = 1

If PDF::Create(#PDF)
  
  PDF::AddPage(#PDF)
  
  PDF::EmbedFont(#PDF,"c:\windows\fonts\BAHNSCHRIFT.TTF", "bahc"); There is something wrong with loading other font faces like "condensed" or "Bold"
  PDF::EmbedFont(#PDF,"c:\windows\fonts\BROADW.TTF", "bro","U")  ;  "U"  is the style here, but it seems to ignore it 
  PDF::EmbedFont(#PDF, "l_10646.ttf", "Luci", "", PDF::#Unicode); unicode example
  
  PDF::SetFont(#PDF, "bahc", "", 16)
  PDF::PlaceText(#PDF, "This is an embedded TrueType-font (BAHNSCHRIFT)", 10, 20) 
  
  PDF::SetFont(#PDF, "bro", "", 16)
  PDF::PlaceText(#PDF, "This is an embedded TrueType-font (BROADWAY)", 10, 35) 
  
  PDF::SetFont(#PDF, "Luci", "", 14)
  PDF::PlaceText(#PDF, "This is an embedded Unicode-font (LucidaSans)", 10, 50)
  
  PDF::SetFont(#PDF, "Luci", "U", 11)
  PDF::PlaceText(#PDF, "This is an embedded Unicode-font  (LucidaSans Underlined)", 10, 60)
  
  PDF::Close(#PDF, File$)
  
EndIf

RunProgram(File$)
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

Thank you for the response. My users can choose a font using a font requester. The older PurePDF can set a font only using the name (I know LoadFont is involved) so I had hoped pbPDF could as well.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: [Module] pbPDF-Module

Post by DeanH »

I have tried to embed Comic Sans MS. Filename is c:\windows\fonts\comic.ttf. It shows courier instead. Am I missing something?

Code: Select all

XIncludeFile "..\pbPDFModule.pbi"

Define File$="pbPDF-EmbedFont-1.pdf"

#PDF = 1

If PDF::Create(#PDF)
  
  PDF::AddPage(#PDF)
  
  PDF::EmbedFont(#PDF,"c:\windows\fonts\BAHNSCHRIFT.TTF", "bahc")
  PDF::EmbedFont(#PDF,"c:\windows\fonts\comic.ttf", "Comic")
  
  PDF::SetFont(#PDF, "bahc", "", 16)
  PDF::PlaceText(#PDF, "This is an embedded TrueType-font (BAHNSCHRIFT)", 10, 20) 
  
  PDF::SetFont(#PDF, "Comic", "", 16)
  PDF::PlaceText(#PDF, "This is an embedded TrueType-font (Comic Sans MS)", 10, 35)
    
  PDF::Close(#PDF, File$)
  
EndIf

RunProgram(File$)

BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: [Module] pbPDF-Module

Post by BarryG »

DeanH wrote: Thu Sep 16, 2021 1:29 amI have tried to embed Comic Sans MS. Filename is c:\windows\fonts\comic.ttf. It shows courier instead. Am I missing something?
Your code snippet works here with the Comic Sans MS font (see below). Maybe your PDF viewer is the problem?

Image
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: [Module] pbPDF-Module

Post by normeus »

I tried Adobe reader and it displayed the font. I know you typed the path but, check PDF errors specially when you are using user provided input:

Code: Select all

PDF::EmbedFont(#PDF,  "c:\windows\fonts\comic.TTF", "Comic")
Debug PDF::GetErrorMessage(#pdf) ; instead of debug you should notify the user that the font is not available
; if a font file is not found, it will default to courier
I tried a font called "Elephant" all my PDF readers worked except for Adobe which gave me an error. You might not want to allow your users to use any font.


Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply