Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi How to Save PDF from web (https://www.delphipraxis.net/52852-how-save-pdf-web.html)

Delphi-Lover 5. Sep 2005 14:17


How to Save PDF from web
 
Hello,

I'm looking for a way to load a PDF from a web URL and then save it to the local disk automatically. I've used the Adobe TPdf control imported in Delphi as an ActiveX control. With this control I can just put in a URL with the PDF address, but I see NO Save function in the TPdf object.... On screen, after loading, in the control there is a save button that will save the document, but I want to save the document in Delphi after loading without a user pressing this save button.

Anyone know how this works with TPdf?
Maybe another way to do this (with TWebbrowser) ??

Greets,

Delphi-Lover.

marabu 5. Sep 2005 14:29

Re: How to Save PDF from web
 
Hello Delphi-Lover,

if you don't insist on viewing the pdf document in question you could download it to your disk with this handy routine from the net (never tried myself):

Delphi-Quellcode:
uses Wininet;

function GetInetFile(const fileURL, FileName: String): boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
  Result:=False;
  sAppName := ExtractFileName(Application.ExeName);
  hSession := InternetOpen(
    PChar(sAppName),
    INTERNET_OPEN_TYPE_PRECONFIG,
    nil, nil, 0
  );
  try
    hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0);
    try
      AssignFile(f, FileName);
      Rewrite(f,1);
      repeat
        InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
        BlockWrite(f, Buffer, BufferLen)
      until BufferLen = 0;
      CloseFile(f);
      Result:=True;
    finally
      InternetCloseHandle(hURL)
    end
  finally
    InternetCloseHandle(hSession)
  end
end;
Wish you success

marabu

Delphi-Lover 5. Sep 2005 14:39

Re: How to Save PDF from web
 
Hallo Marabu,

Many Thanks!! :love:

The code works OK and I can use it as it is.

Maybe interesting to know if this is also
possible with the TPdf control. anyone????

Greetings,

Delphi_lover

Bernhard Geyer 5. Sep 2005 14:50

Re: How to Save PDF from web
 
Zitat:

Zitat von Delphi-Lover
Maybe interesting to know if this is also
possible with the TPdf control. anyone????

No, it isn't. The Interface that is provided by Adobe for the Adobe Viewer is a minimum to let the control work in the IE. Nothing else is supported by Adobe, so only very few Methodes are exported in the COM-Interface.

marabu 5. Sep 2005 16:08

Re: How to Save PDF from web
 
Hi folks,

I just imported the Adobe Acrobat 7.0 Browser Control Type Library 1.0 just to find out what Bernhard mentioned - the interface is a very scarce resource. But although the requested functionality is not exported directly by the COM interface, the functions are there all the same.

The Browser Control is perfectly capable to write pdf documents to your hard drive. By processing this tiny piece of code the well known toolbar is exposed and you can save a copy of the document:

Delphi-Quellcode:
procedure TDemoForm.DemoButtonClick(Sender: TObject);
begin
  with AcroPdf do begin
    setShowToolbar(true);
    LoadFile('c:\daten\download.pdf');
  end;
end;
Since the LoadFile() method does not accept a URL, I conclude, that the Internet Explorer does the download part and knows a way to inform AcroPdf. Those who have access to the Acrobat SDK should be able to find out about the other interfaces supported via AcroPdf.ControlInterface or the command identifiers handled by AcroPdf.DoObjectVerb().

The rest of us can use the function GetInetFile() posted above. Or give the indy http client a try:

Delphi-Quellcode:
procedure Download(url, filename: string);
var
  fs: TFileStream;
  http: TIdHttp;
begin
  fs := TFileStream.Create(fileName, fmCreate);
  http := TIdHttp.Create(nil);
  http.Get(url, fs);
  http.Free;
  fs.Free;
end;
So long

marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:19 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz