EvotecIT / PSWritePDF

PowerShell Module to create, edit, split, merge PDF files on Windows / Linux and MacOS
GNU Affero General Public License v3.0
190 stars 21 forks source link

Register-PDFFont problem? #18

Closed MVKozlov closed 3 years ago

MVKozlov commented 3 years ago

When I try to use new font feature I meet a problem when repeat pdf creation

PS D:\> Register-PDFFont -FontName 'Verdana' -FontPath 'C:\Windows\fonts\verdana.ttf' -Encoding IDENTITY_H -Cached -Default
PS D:\> New-PDF -FilePath d:\2.pdf -PDFContent { New-PDFText -Text 'Hello ', 'Привет !' }
PS D:\> New-PDF -FilePath d:\2.pdf -PDFContent { New-PDFText -Text 'Hello ', 'Привет !' }
MethodInvocationException: D:\PowerShell\Modules\PSWritePDF\0.0.11\PSWritePDF.psm1:994
Line |
 994 |          $Script:Document.Close();
     |          ~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Close" with "0" argument(s): "Pdf indirect object belongs to other PDF document. Copy object to current
     | pdf document."

May be I use it wrong ?

PrzemyslawKlys commented 3 years ago

Register-PDFFont must be within New-PDF.

PrzemyslawKlys commented 3 years ago
New-PDF -FilePath "$PSScriptRoot\Example01_Simple1.pdf" -PDFContent {
    Register-PDFFont -FontName 'Verdana' -FontPath 'C:\Windows\fonts\verdana.ttf' -Encoding IDENTITY_H -Cached -Default
    New-PDFText -Text 'Hello ', 'Привет !'
} -Show
New-PDF -FilePath "$PSScriptRoot\Example01_Simple2.pdf" -PDFContent {
    Register-PDFFont -FontName 'Verdana' -FontPath 'C:\Windows\fonts\verdana.ttf' -Encoding IDENTITY_H -Cached -Default
    New-PDFText -Text 'Hello ', 'Привет !'
} -Show
MVKozlov commented 3 years ago

OK I understood. but Register-PDFFont really needs help text :)

While testing your example, I ran into another problem PS just closes instead of creating a file. It seems because it cannot write to C: \ (I am running your code from cmdline, not from script)

Should I create a separate issue?

PrzemyslawKlys commented 3 years ago

So you mean when the file cannot be saved because you chose the wrong file path it should save to temp file instead? It's something to think about - but the last time I tried to detect this it wasn't working.

MVKozlov commented 3 years ago

No, I mean it should not close powershell window but write error instead :) I found message like "Application closed because 0xc0000005" in application event log

PrzemyslawKlys commented 3 years ago

I don't know if I can control this. I've seen this a few times and I couldn't "catch" it. It's like it crashes whole NET and closes PowerShell

PrzemyslawKlys commented 3 years ago

Here's the same issue: https://github.com/EvotecIT/PSWritePDF/issues/14

MVKozlov commented 3 years ago

Seems I found it

function New-InternalPDF {
....
catch
...
exit
...
}

You should use Write-Error, throw or return instead of "exit" because exit closes current session

PrzemyslawKlys commented 3 years ago

That's my mistake of using Exit instead of return. Will fix.

PrzemyslawKlys commented 3 years ago

Please test new version

MVKozlov commented 3 years ago
PS D:\> New-PDF -FilePath "C:\Example01_Simple2.pdf" -PDFContent {}
WARNING: New-InternalPDF - Terminating error: Exception calling ".ctor" with "1" argument(s): "Access to the path 'C:\Example01_Simple2.pdf' is denied."
Remove-EmptyValue: D:\PowerShell\Modules\PSWritePDF\0.0.12\PSWritePDF.psm1:267
Line |
 267 |          Remove-EmptyValue -Hashtable $Splat
     |                                       ~~~~~~
     | Cannot bind argument to parameter 'Hashtable' because it is null.

If content not emply we gef a bunch of "You cannot call a method on a null-valued expression." I think throw will be better :) or there is a need $null checking

PrzemyslawKlys commented 3 years ago

I've improved error handling.

MVKozlov commented 3 years ago

still not perfect

PS D:\> New-PDF -FilePath "C:\Example01_Simple2.pdf" -PDFContent { New-PDFText -Text 'Hello ', 'Привет !' }
WARNING: New-InternalPDF - Terminating error: Exception calling ".ctor" with "1" argument(s): "Access to the path 'C:\Example01_Simple2.pdf' is denied."
InvalidOperation: D:\PowerShell\Modules\PSWritePDF\0.0.13\PSWritePDF.psm1:470
Line |
 470 |          $null = $Script:PDF.AddNewPage()
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

MethodInvocationException: D:\PowerShell\Modules\PSWritePDF\0.0.13\PSWritePDF.psm1:472
Line |
 472 |      $Script:Document = [iText.Layout.Document]::new($Script:PDF)
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object."

InvalidOperation: D:\PowerShell\Modules\PSWritePDF\0.0.13\PSWritePDF.psm1:470
Line |
 470 |          $null = $Script:PDF.AddNewPage()
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

MethodInvocationException: D:\PowerShell\Modules\PSWritePDF\0.0.13\PSWritePDF.psm1:472
Line |
 472 |      $Script:Document = [iText.Layout.Document]::new($Script:PDF)
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object."

InvalidOperation: D:\PowerShell\Modules\PSWritePDF\0.0.13\PSWritePDF.psm1:22
Line |
  22 |          $null = $Script:Document.Add($Object)
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.