Webapplication (console) exports blurry fonts in PDF

Hi,

I would like to use the console web application for generating a PDF.

The fonts are stored in the same folder as the webapplication and PAKO Zip Lib is activated but the saved PDF document is still blurred.

program qsg;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  WEBLib.TMSFNCPDFLib, WEBLib.TMSFNCTypes, WEBLib.TMSFNCGraphicsTypes,
  browserconsole;

procedure DoFontCacheReady(Sender: TObject);
begin

end;

var
  p: TTMSFNCPDFLib;
  r: TRectF;
  s: string;
begin
  AddFontToCache('ARIAL.TTF');
  AddFontToCache('TAHOMA.TTF');

  p := TTMSFNCPDFLib.Create;

  try
    s := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy'+
      'text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. '+
      'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It'+
      ' was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with des'+
      'ktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
    p.BeginDocument();
    p.NewPage;

    p.Graphics.Font.Name := 'Arial';
    p.Graphics.Font.Size := 14;
    p.Graphics.Fill.Color := gcNull;
    r := RectF(10, 50, 500, 400);
    p.Graphics.DrawRectangle(r);
    p.Graphics.DrawText(s, r, 3);
    p.EndDocument(True).SaveToFile('test4.pdf');
  finally
    p.Free;
  end;
end.

Any idea why the PDF doesn't have normal lettertype?

The AddFontToCache is asynchronous, you need to await the font cache.

program Project40;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  WEBLib.TMSFNCPDFLib, WEBLib.TMSFNCTypes, WEBLib.TMSFNCGraphicsTypes, WEBLib.Forms,
  browserconsole;

type
  TExport = class
    class procedure DoFontCacheReady(Sender: TObject);
  end;

{ TExport }

class procedure TExport.DoFontCacheReady(Sender: TObject);
var
  p: TTMSFNCPDFLib;
  r: TRectF;
  s: string;
begin
  p := TTMSFNCPDFLib.Create;

  try
    s := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy'+
      'text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. '+
      'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It'+
      ' was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with des'+
      'ktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
    p.BeginDocument();
    p.HeaderFont.Name := 'Roboto';
    p.FooterFont.Name := 'Roboto';
    p.PageNumberFont.Name := 'Roboto';

    p.NewPage;

    p.Graphics.Font.Name := 'Roboto';
    p.Graphics.Font.Size := 14;
    p.Graphics.Fill.Color := gcNull;
    r := RectF(10, 50, 500, 400);
    p.Graphics.DrawRectangle(r);
    p.Graphics.DrawText(s, r, 3);
    p.EndDocument(True).SaveToFile('test4.pdf');
  finally
    p.Free;
  end;
end;

begin
  Application.OnFontCacheReady := @TExport.DoFontCacheReady;
  AddFontToCache('https://download.tmssoftware.com/tmsweb/pdf/fonts/Roboto-Regular.ttf');
  AddFontToCache('https://download.tmssoftware.com/tmsweb/pdf/fonts/Roboto-Bold.ttf');
  AddFontToCache('https://download.tmssoftware.com/tmsweb/pdf/fonts/Roboto-BoldItalic.ttf');
  AddFontToCache('https://download.tmssoftware.com/tmsweb/pdf/fonts/Roboto-Italic.ttf');
end.

Hi Pieter,

Thank you for the info. It works like a charm!

Regards,

Michael

2 Likes

Hi,

I would like to add an image to the PDF however the error message 'Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.' appears.

p := TTMSFNCPDFLib.Create;

bc := TTMSFNCBitmapContainer.Create;
bc.AddFromURL('MyImage.jpg', 'MyImage');

try
p.BitmapContainer := bc;

s := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy'+
  'text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. '+
  'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It'+
  ' was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with des'+
  'ktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
p.BeginDocument();
p.HeaderFont.Name := 'Roboto';
p.FooterFont.Name := 'Roboto';
p.PageNumberFont.Name := 'Roboto';

p.NewPage;

p.Graphics.Font.Name := 'Roboto';
p.Graphics.Font.Size := 14;
p.Graphics.Fill.Color := gcNull;
r := RectF(10, 50, 500, 400);
p.Graphics.DrawRectangle(r);
p.Graphics.DrawText(s, r, 3);
p.Graphics.DrawImageWithName('MyImage', PointF(100, 100));
p.EndDocument(True).SaveToFile('test4.pdf');

finally
bc.Free;
p.Free;

end;

What is the proper way to load an image from an URL (localhost) and draw it in a PDF file?

Hoi Pieter, could you please give some suggestions how to do this?

This should work, but note that adding an image is asynchronous in a web environment. It might be possible that the image is not yet fully loaded when you are calling DrawImageWithName. The OnBitmapChanged event on TTMSFNCBitmapContainer should help with that.

Thanks for the help. Using OnBitmapChanged event did the trick.

Have a nice weekend! :wink:

2 Likes