TTMSFNCPDFLib

Hi,

When using TTMSFNCPDFLib with Web Core, how do you display the PDF after creating it?

Thanks,

Ken
Ok, I have realised that EndDocument(True) does this but using the following produces garbage. Can you please tell me where I am going wrong?

 PDF:=TTMSFNCPDFLib.Create;
  try
    PDF.BeginDocument('VATReturn.pdf');
    PDF.PageSize:=psA4;
    PDF.Header:='VAT Return';
    PDF.Footer:='Page 1/1';
    PDF.NewPage;
    RH:=PDF.GetHeaderRect;
    PDF.Graphics.Font.SizeNoScale:=12;
    PDF.Graphics.Font.Color:=gcBlack;
    PDF.Graphics.Font.Style:=[];
    TR:=PDF.Graphics.DrawText('Drawing primitives', PointF(RH.Left,RH.Bottom+10));
    PDF.EndDocument(True);
  finally
    PDF.Free;
  end;
I should add that what I am seeing generated in the PDF, for header,footer and body is all characters in each of the three strings overwriting each other at the same single position.

Additionally, when the PDF is displayed the tab shows it as a andom sequence.pdf.pdf not the filename I allocated in the BeginDocument.

Hi,


You need to add fonts that you are using in the PDF in order to see the characters rendered normal.
The default font that is used is 'Arial'. This can be upload ttf files to the server and using the following code:



  Application.OnFontCacheReady := DoFontCacheReady;
  AddFontToCache('https://myserver.com/tmsweb/pdf/fonts/arial.ttf');
  AddFontToCache('https://myserver.com/tmsweb/pdf/fonts/tahoma.ttf');


the OnFontCacheReady will tell you when the fonts are pre-loaded and you can start generating your PDF. Alternatively, you can also add ttf files to your project, which will be copied to the output directory. And then you can add them via the following code:



var
  pth: string;
begin
  asm
    var loc = window.location.href;
    var dir = loc.substring(0, loc.lastIndexOf('/'));
    pth = dir;
  end;


  AddFontToCache(pth + '/Fonts/arial.ttf');
end;

Thanks Pieter.

I have already added a ttf to the project. When should the last block of code be executed in relationship to the code to generate the PDF? Does it only have to be executed once?

Thanks,

Ken


the AddFontToCache should only be executed once, and when the DoFontCacheReady is triggered, you can start exporting to PDF. We have upload a sample that demonstrates this, based on your code. Additionally, the code you are using, will try to open the PDF in a PDF reader internally in the browser. If you have an AD Blocker, then it could be possible that you will not see anything. Alternatively, you can also save directly to the Downloads folder, as if you were downloading a file, or if your internet browser is configured to prompt to user with a save dialog, it should present a save dialog.


http://www.tmssoftware.net/public/webcore/PDFExportSample.zip

Thanks, I'll check out the sample.

Perfect. Thank you very much.

Your welcome!

One question though. How do I set the font for the PDF in Web Core?

You can set


PDF.Graphics.Font.Name := 'Arial'; or 
PDF.Graphics.Font.Name := 'Tahoma';

It depends on the loaded TTF files, or you can also change the header and footer font with:

PDF.HeaderFont.Name := 'Tahoma';
PDF.FooterFont.Name := 'Tahoma';

I can't get this to work with a ttf added to the project. I have modified your sample and uploaded it to http://test.simplyaccounts.net/PDFExportSample.zip I would appreciate it if you could tell me what I am doing wrong.

Please change the code to:


As you have not loaded a default font 'Arial', you need to clear the font fall back list and use Gisha as a font fallback



procedure TForm59.WebButton1Click(Sender: TObject);
var
  PDF: TTMSFNCPDFLib;
  RH: TRectF;
  TR: TRectF;
  ms: TMemoryStream;
  nFont: string;
begin
  nFont := 'Gisha';
  PDF := TTMSFNCPDFLib.Create;
  try
    PDF.FontFallBackList.Clear;
    PDF.FontFallBackList.Add(nFont);
    PDF.BeginDocument{('VATReturn.pdf')};
    PDF.FooterFont.Name:=nFont;
    PDF.HeaderFont.Name:=nFont;
    PDF.PageSize := psA4;
    PDF.Header := 'VAT Return';
    PDF.Footer := 'Page 1/1';
    PDF.NewPage;
    RH := PDF.GetHeaderRect;
    PDF.Graphics.Font.Name:=nFont;
    PDF.Graphics.Font.SizeNoScale:=12;
    PDF.Graphics.Font.Color := gcBlack;
    PDF.Graphics.Font.Style := [];
    TR := PDF.Graphics.DrawText('Drawing primitives', PointF(RH.Left, RH.Bottom + 10));
    ms := PDF.EndDocument{(True)};
    try
      ms.SaveToFile('VATReturn.pdf');
    finally
      ms.Free;
    end;
  finally
    PDF.Free;
  end;
end;

Thanks again. Getting much closer. Adobe Reader displays a popup saying "Cannot extract the embedded font 'Gisha'. Some characters may not display or print correctly". This also happens in your original sample using the fonts from your website.

This is unfortunately a common issue, when displaying the same PDF in Foxit Reader then the message does not display. We have added this on our todolist for investigation, but could potentially be due to a false positive reported in Adobe Reader.


https://forums.adobe.com/thread/1026259
Thanks. Have managed to generate the report I need fine. The only problem is that a £ symbol will not display correctly even if I use £

If a £ symbol is in a string garbage is displayed.

It's possible that the font you are using is not containing all symbols. When looking at the characters that are included at https://www.fontpalace.com/font-details/Gisha/ I see that it does not include '£'

That's strange as I use Gisha for the entire app and the £ sign is displayed fine in lots of places.

http://test.simplyaccounts.net/Gisha.png

My Bad, we forgot a crucial part here. Whenever a unicode character is used, the font is added compressed inside the PDF. For compression, pako is used, an opensource javascript compression library. This is not automatically included in your project. To include this you need to right-click the project and then select "Manage JavaScript Libraries". You'll get a popup that shows the supported libraries, then you can check "Pako ZLib Compression (PDF)". Click on Apply, and then OK. Clean, Rebuild your application and then refresh the browser application.