[Lazarus] TProcess pipe through StdIn

Leonardo M. Ramé l.rame at griensu.com
Fri Aug 17 21:27:29 CEST 2012


I'm using xhtml2pdf to convert html text to pdf. If I use from the
command line, I can do this:

cat /var/www/cgi-bin/salida.html | xhtml2pdf - -

or this:

xhtml2pdf - - < /var/www/cgi-bin/salida.html

The parameters "- -" mean, take input from StdIn and send output to
StdOut.

When I try to execute the same using TProcess, it apparently never
receives the input.

Here's a modified snippet I copied from http://wiki.freepascal.org/Executing_External_Programs 

procedure TForm1.Button1Click(Sender: TObject);
var
  lProcess: TProcess;
  lStr: TStringList;
  lText: string;

begin
  lProcess := TProcess.Create(nil);
  lStr := TStringList.Create;
  try
    lStr.LoadFromFile('/var/www/cgi-bin/salida.html');
    lProcess.Options := [poUsePipes, poStderrToOutPut];
    lProcess.CommandLine := '/usr/bin/xhtml2pdf - -';
    lProcess.Execute;
    lText := lStr.Text;
    lProcess.Input.Write(lText[1], Length(lText));

    lStr.Clear;
    lStr.LoadFromStream(lProcess.Output);
    lStr.SaveToFile('output.pdf');
  finally
    lStr.Free;
    lProcess.Free;
  end;
end; 
-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Lazarus mailing list