WPCubed Product Support

Convert RTF to PDF

wpsupport

I assume the problem is caused by the text not to be formatted.

  1. I recommend to create a TWPCustomRTFEditclass instead of TWPRichText.
  2. Instead of Create(nil) call CreateDynamic. The latter makes sure the control works without a parent form.
  3. Call ReformatAlltwice before Print, not before and not Refresh.

Use the component in a DLL is a problem? I neep only the PDF convertion then I set visible = False;

That can be a problem since this also potentially violates the license.

#2 ♥ 0
1.4k Views 2 users read 3 min

I need do conver a RTF file to PDF.

Its works fine til I update the wPDF component to version 5.

My RTF was created with WPTool6 from another application that we cant to update to WPTool9...

But the result PDF generated - with RTF by WPTool6 e converted to PDF with wPDF5 - have font encode problems.

Then I tried develeper a DLL with Delphi 12 - with WPTool9 and wPDF5 - where I get another problem: beffore the convertion, I need to put a image at the top of document, but the image dont appears in a PDF file. If I compile a test .exe app and try to execute the same code, it works fine, the image appears.

Use the component in a DLL is a problem? I neep only the PDF convertion then I set visible = False;

Example 1 (no access violation):

procedure TConvertToPDF.Convert(pFileNameIn, pFileNameOut, pParams: PAnsiChar);
var
 WPRichText : TWPRichText;
 WPPDFExport: TWPPDFExport;
begin
WPDF_Start(wPDFName, WPDFCode);

WPRichText  := TWPRichText.Create(nil);
try
 WPRichText.Visible            := False;
 WPRichText.LoadFromFile(pFileNameIn);
 WPRichText.ReformatAll();

 WPRichText.MovePosition(wpmHome, False);
 WPRichText.FastInsertParagraph;

 WPRichText.MovePosition(wpmHome, False);
 WPRichText.FastInsertParagraph;

 WPRichText.InsertGraphic('D:\Desenv\VilaRica\Docs\Logotipo\logo.bmp', False, []); // access violation here

 WPRichText.Refresh;

 // Export to PDF
 WPPDFExport := TWPPDFExport.Create(nil);
 try
   WPPDFExport.Source   := WPRichText;
   WPPDFExport.FileName := pFileNameOut;
   WPPDFExport.Print;
 finally
   FreeAndNil(WPPDFExport);
 end;
finally
  FreeAndNil(WPRichText);
end;
end;

Example 2 (no access violation, but no add the image):

procedure TConvertToPDF.Convert(pFileNameIn, pFileNameOut, pParams: PAnsiChar);
var
 WPRichText : TWPRichText;
 WPPDFExport: TWPPDFExport;
begin
WPDF_Start(wPDFName, WPDFCode);

WPRichText  := TWPRichText.Create(nil);
try
 WPRichText.Visible            := False;
 WPRichText.OnRequestHTTPImage := RequestHTTPImage;
 WPRichText.LoadFromFile(pFileNameIn);
 WPRichText.ReformatAll();

 WPRichText.MovePosition(wpmHome, False);
 WPRichText.FastInsertParagraph;

 WPRichText.MovePosition(wpmHome, False);
 WPRichText.FastInsertParagraph;

 WPRichText.InsertGraphic('D:\Desenv\VilaRica\Docs\Logotipo\logo.bmp', True, []);

 WPRichText.Refresh;

 // Export to PDF
 WPPDFExport := TWPPDFExport.Create(nil);
 try
   WPPDFExport.Source   := WPRichText;
   WPPDFExport.FileName := pFileNameOut;
   WPPDFExport.Print;
 finally
   FreeAndNil(WPPDFExport);
 end;
finally
  FreeAndNil(WPRichText);
end;
end;

procedure TConvertToPDF.RequestHTTPImage(RTFData: TWPRTFDataCollectionBase; Reader: TWPCustomTextReader; const LoadPath, url: string; TextObject: TWPTextObj; var Ok: Boolean);
begin
TextObject.LoadObjFromFile(url);

Ok := True;
end;

#1 ✎ ♥ 0