NewPage method bug ?

Hi

I am starting to test the TTMSFNCPDFLib componant.

Into my "big" appli, I wrote a small procedure, very very basic one.

 procedure TfMigration.Button30Click(Sender: TObject);
 var
 pdf : TTMSFNCPDFLib;
 
 begin
      TRY
         pdf := TTMSFNCPDFLib.Create;
         pdf.BeginDocument('test.pdf')  ;
         pdf.NewPage;
 
         pdf.Graphics.Stroke.Color := gcBlack;
         pdf.Graphics.Stroke.Width := 2;
        pdf.EndDocument(true);
      FINALLY
         pdf.Free;
      END;   // Fin de FINALLY
 end;

Very basic isn't it ;)

However, at "pdf.NewPage;", I received an exception you can see below.

image

You have an idea, what I missed ?

regards
olivier

Hi,

We cannot reproduce this here. Test in VCL & FMX. Which framework are you using? I noticed you are using GExperts. Is the issue happening without GExperts? Are you able to reproduce this in a new application?

Hi
Sorry, I forget to indicate you that I can run your demo about it without pb

I tried without geexpert, and the same pb occurs

When I break the app, I reached this procedure :
TTMSFNCGeneralPDFLibFontInitializer.InternalInitializeCharWidths

at the line 756 = FCharWidths[ndx].g := gidx;

if it helps

olivier

Which framework? Can you put together a sample that reproduces the issue?

I use it as VCL for a windows 10 32bits app.

As I said I can run your demo so Iam not convicted I can reproduce this behaviour into a specific project :frowning:

I can joined the unit I used in this form, if it's help (most of them are yours ;) )

USES
    Windows,
    Messages,
    SysUtils,
    shlobj,
    Classes,
    Graphics,
    Controls,
    Forms,
    Dialogs,
    DateUtils,
    Types,
    Db,
    Commun,
    Registre,
    AdvCGrid,
    StrUtils,
    Coordonnee,
    RTTI,
    Data.SqlTimSt,
    // ESBPCSConvert, // ExtractValue
    AdvSmartMessageBox,
    ExeInfo,
    plsLangMan,
    SLT.Common.TextFileReader,
    VCL.TMSLogging,

    AdvGrid,
    Variants,
    TaskDialog,
    Xmldom,
    XMLIntf,
    Msxmldom,
    XMLDoc,
    AdvOfficeStatusBar,
    AdvAppStyler,
    AdvEdit,
    JvMail,
    JvComponentBase,
    JvThread,

    AdvAlertWindow,
    BaseGrid,
    _UFlxMessages.FlxMessages,
    _UFlxNumberFormat.TFlxNumberFormat,
    FlexCel.Render,
    cabfiles,

    VCL.FlexCel.Core,
    FlexCel.Pdf,
    FlexCel.XlsAdapter,
    JvAlarms,
    WUpdateWiz,
    WUpdateLanguagesU,
    WUpdate,
    IdBaseComponent,
    IdComponent,
    IdCustomTCPServer,
    IdTCPServer,
    TMSLoggingTCPOutputHandler,
    TMSLoggingUtils,
    TMSLoggingCore,
    UAdvGridExcelImport,
    UAdvGridExcelExport,
    VCL.ExtCtrls,

    TMSLoggingCSVOutputHandler,
    TMSLoggingTextOutputHandler,
    TMSLoggingHTMLOutputHandler,
    TMSLoggingBrowserOutputHandler,
    TMSLoggingEventLogOutputHandler,
    AdvStyleIF,
    //
    FireDAC.Stan.Intf,
    FireDAC.Stan.Option,
    FireDAC.Stan.Param,
    FireDAC.Stan.Error,
    FireDAC.DatS,
    FireDAC.Phys.Intf,
    FireDAC.DApt.Intf,
    FireDAC.Stan.Async
       ,
    FireDAC.DApt,
    FireDAC.UI.Intf,
    FireDAC.Stan.Def,
    FireDAC.Stan.Pool,
    FireDAC.Phys,
    FireDAC.Phys.FB,
    FireDAC.Phys.FBDef,
    FireDAC.Comp.Client,
    FireDAC.Comp.DataSet,
    FireDAC.Phys.IBBase,
    FireDAC.Phys.PGDef,
    FireDAC.Phys.PG,
    FireDAC.Comp.ScriptCommands,
    FireDAC.Stan.Util,
    FireDAC.Comp.Script,
    FireDAC.Phys.IBWrapper,
    FireDAC.VCLUI.Wait,
    FireDAC.Comp.UI,
    IdThreadComponent,
    Planner, FireDAC.Stan.StorageBin, FireDAC.Stan.StorageXML ,
    Sempare.Template,  VCL.TMSFNCMapsCommonTypes ,VCL.TMSFNCGeocoding, VCL.TMSFNCMaps,  //
         Vcl.TMSFNCUtils, VCL.TMSFNCPDFLib, vcl.TMSFNCGraphics, vcl.TMSFNCPDFCoreLibBase
   ;

Just more info

1 - I created a new form into my app containing the basic unit (just to use the pdflid)
and the same problem occurs

2 - I created a new project to create pdf but containing all unit from my project, and it works

not very useful to you

sorry

Such issues are always hard to reproduce when they are occuring in a larger application. I suggest to comment parts of your application that might affect the PDF export, maybe some units that are included?

Hi

I tried to do that, but I didn't sucess to track the problem

any idea ?

regards

I don't see immediately what the cause for this issue would be. I cannot reproduce it here in a separate example so it's unclear how we can identify the issue and start searching for a solution?

HI

I tracked a little from NewPage.

I track this :

  • Unit : TTMSFNCCustomPDFLib
    at procedure TTMSFNCCustomPDFLib.NewPage;
    FPDFLib.NewPage;

  • Unit VCL.TMSFNCPDFLib.General
    at Procedure TTMSFNCGeneralPDFLib.NewPage;
    line 1078 : UpdateFontList;

  • Unit VCL.TMSFNCPDFLib.General
    procedure TTMSFNCGeneralPDFLib.UpdateFontList(ASearchForUnicodeFont: Boolean = False);
    line 3534 : ft.Initializer.InitializeCharWidths;

  • unit VCL.TMSFNCPDFLib.General.Win;
    procedure TTMSFNCGeneralPDFLibFontInitializer.InitializeCharWidths;
    Line 312 : InternalInitializeCharWidths(FMainInitializer.FDocumentHandle);

  • unit VCL.TMSFNCPDFLib.General.Win;
    procedure TTMSFNCGeneralPDFLibFontInitializer.InternalInitializeCharWidths(
    ADocumentHandle: HDC);
    line 740 (I write an Try except inside to give some detail)

                for i := 0 to n - 1 do
                begin
                  le_i := i;
                  id := ida^[i];
                  gi := idr^[i];
                  if gi <> 0 then
                    gi := gi shr 1 + i - n - sc^[i];

                  for c := sc^[i] to ec^[i] do
                  begin
                   le_c := c;
                    FCharArray.v[ndx] := c;
                    if gi = 0 then
                      gidx := c + id
                    else
                    begin
                      gidx := gia^[gi + c];
                      if gidx <> 0 then
                        Inc(gidx, id);
                    end;

                    **FCharWidths[ndx].g := gidx;**
                    inc(ndx);
                  end;
                end;

i = 135
n = 163
id = 26996
gi = 0
for c := sc^[i] to ec^[i] do with
- sc^[i] = 42786
- ec^[i] = 42887
c = 42786
gidx = 69782

FCharWidths[ndx].g := gidx;
g is supposed to be a word ( TTMSFNCPDFGraphicsLibFontCharWidth = record
w: word;
g: word;
i: Integer;
end;)

whereas gidx is an Integer.

That's help you ?
regards
olivier

I know what the code does, but again, it's not reproducible in a separate example which means that in your main application there is something that affects memory, which affects the pdf lib generation. We are more than willing to help out but unfortunately, we can not start guessing what patch to apply. As long as we don't have a reproducible case it's almost impossible to know where to start looking. Therefore I suggest to break down your application, commenting pieces of code until the issue no longer occurs, in order to pin down exactly which code has effect and which units have effect. If you can exactly pinpoint which combination is the cause for this issue, we know where to look and maybe find a solution...

I totally agree with you. This is just to show you where the pb orccurs, but I know that if you don't know where the source is, it's difficult to correct, even dangerous.

But at the moment I don't see how I can short-circuit some parts of my code. I'll look into it, but for the moment I don't have the tools to do it. I'll keep you posted.

Note, I changed the type of the FCharWidths record from word to Integer, and I can create the PDF. But as expected this impacts other parts. So, using the code from your demo, some UTF8 text makes the app crash.

It is :
// p.Graphics.DrawHTMLText('Japanese: こんにちは世界', PointF(rh.Left + 210, tr.Bottom + 30));
// p.Graphics.DrawHTMLText('Telugu: హలో వరల్డ్', PointF(rh.Left + 210, tr.Bottom + 50));
// p.Graphics.DrawHTMLText('Lao: ສະ​ບາຍ​ດີ​ຊາວ​ໂລກ', PointF(rh.Left + 410, tr.Bottom + 50));

for the other language it is ok, even for Thai. May be it will help you

regards
olivier

Thank you for your understanding! Changing from Word to Integer will indeed impact the generation as this points to a different piece of memory as Integer is larger in bytes than Word. Let me know how things go with your research, I'm more than happy to help out in any way possible.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.