Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open a PDF file in a new window
#1
Hi All,

I have changed the post completely as I now have a partly working solution:

I have managed to get a PDF file opened in a separat window, but only by copying the file into the wwwroot (contentpath) directory. When the file is there, WebApplication.NewWindows({filename}) will work. Both on IE11 and Edge.

However, the files I want to be able to open and show, are all on a document server (+/- 2 mill. files), and the program have access to the area. A function in the program checks the presence of the file before it allows the user to click a button to open and see the file. However, when WebApplication.NewWindow is called with a filepath, outside the wwwroot path, the browser that opens up, give an error 404.

So what does it take for the WA.NewWindow to be allowed to open a file on a shared network resource, outside the wwwroot path, but within the domain the webservice server is in ?

Obviously it works when the file is in the wwwroot directory, so a solution could be to copy the requested file over to wwwroot, show the file on screen, and then delete the copy after viewing it. The files are max. 5k each, so size is not a problem. But how do I copy it over ? FileExists(exactfilepath) works, but can I use something like XCopy(exactFilepath,contentpath) to copy it and then open it ? Or something similar ?

I've tried with Sendfile to get hold of the file on the document server, and to download it to the wwwroot dir, but sendfile do not work either. I hope someone else have been more sucessful and will explain how to.

Regards
Soren
Reply
#2
You can make a custom content handler which will allow you to map a URL to any place accessible by your IW app, including network drives.
Reply
#3
Hi Kudzu,

Thanks for your reply. I'll see what I can find out. I know there's a demo about custom content handlers. I'll check it out.

In the meantime I have tried to get Filecopy to work, and I admit it does try, but constantly fail, mostly due to be unable to write/create the copied file, but sometimes wit a message like:
"Copy failed. The file could not be created. The path was not found." But which path ? The Fromfile or the Tofile ?

I'v tried all kind of combinations of From and To file names, but no luck so far:

var FileFrom, FileTo : string;
begin
  Filefrom := '\\file\share\100\12087.pdf';
  FileTo := 'files\12087.pdf';   (Files exists under wwwroot which is under Debug where the exefile is.
  FileCopy(FileFrom, FileTo, true);

As FileFrom is on a separate server, I have also tried mapping a drive to the share (u: \\file\share) and so setting FileFrom := 'U:\100\12087.pdf',
Even if FileFrom is from the same disk (C:\preload\12087.pdf) it's not working.

Errors is a mix of failed due to not getting the file, and not being able to create it. It might just be I have not managed to hit the right combination yet...

Regards
Soren
Reply
#4
Where is FileCopy? VCL has many methods for file copy, all of which are well documented:

http://docwiki.embarcadero.com/CodeExamp...s_(Delphi)
Same applies for C++.

Otherwise check permissions and used account. If running as a service or IIS, network drives are not available as mapped letters unless that user has them mapped. Best to use UNC and make sure that user has permissions.
Reply
#5
Hi Soren, try use IW Cache system:
Code:
procedure TIWForm5.IWGradButton1Click(Sender: TObject);
var
  xFileName: string;
  xURL: string;
begin
  // get a new temp file name. This method only returns a file name, the file  is not created
  xFileName := TIWAppCache.NewTempFileName;
  // copy a sample pdf file to our new cache file. We are just simulating a pdf file creation
  FileCopy('your_file.pdf', xFileName, True);
  // add the pdf file to the cache. cache type is defined as ctOneTime, i.e., the file will be deleted when served
  xURL := TIWAppCache.AddFileToCache(Self, xFileName, TIWMimeTypes.GetAsString(mtPDF), ctOneTime);
  // open a new window with our PDF file
  WebApplication.NewWindow(xURL);
end;
From here: https://github.com/Atozed/IntraWeb/blob/.../Unit5.pas
Reply
#6
(10-11-2019, 05:03 PM)kudzu Wrote: Where is FileCopy? VCL has many methods for file copy, all of which are well documented:

http://docwiki.embarcadero.com/CodeExamp...s_(Delphi)
Same applies for C++.

Otherwise check permissions and used account. If running as a service or IIS, network drives are not available as mapped letters unless that user has them mapped. Best to use UNC and make sure that user has permissions.

Hi Kudzu, FileCopy is in the IW.Common.SysTools unit.

(10-11-2019, 05:27 PM)Jose Nilton Pace Wrote: Hi Soren, try use IW Cache system:
Code:
procedure TIWForm5.IWGradButton1Click(Sender: TObject);
var
  xFileName: string;
  xURL: string;
begin
  // get a new temp file name. This method only returns a file name, the file  is not created
  xFileName := TIWAppCache.NewTempFileName;
  // copy a sample pdf file to our new cache file. We are just simulating a pdf file creation
  FileCopy('your_file.pdf', xFileName, True);
  // add the pdf file to the cache. cache type is defined as ctOneTime, i.e., the file will be deleted when served
  xURL := TIWAppCache.AddFileToCache(Self, xFileName, TIWMimeTypes.GetAsString(mtPDF), ctOneTime);
  // open a new window with our PDF file
  WebApplication.NewWindow(xURL);
end;
From here: https://github.com/Atozed/IntraWeb/blob/.../Unit5.pas

Hi Jose,

Thanks for the hint... I have finally managed to get it to work. It has taken me quite a long time but now it's working. Thanks to everyone for guides, hints and help. Without your help I would still have been the dark.

The key was in all of the 4 lines above in your hint, and especially in the FileCopy, where

'your_file.pdf', has been replaced by a filename like '\\File01.corp.company.dk\SalesReceipt\146\146968\14686943-631.pdf'.

One setback though is that it is not working on IE11. It's working fine on Edge, Chrome, Cliqz / Firefox, apart from having to allow popup on them all. I have not tested it on Safari.
On IE11 it opens up a new, but blank screen, and blocks the workstation for a number of seconds. Mostly it is possible to close the new window again, but often it is necessary to close the browser with all open tabs.

I hope I'll find a solution to that, and should anyone have any knowledge of solutions, please let me know. One possible reason, as far as I've read, can be that Acrobat Reader is not installed correctly in relation to IE11. Recommends are to de-install and re-install, but I have not tried that yet.

Anyway, time for a bit of rest now. At least until tomorrow.

Regards
Soren
Reply
#7
Soren, i not tested this but you can do tests with IE. Show your PDF using http://mozilla.github.io/pdf.js/
Reply
#8
Hi Jose,

I've tried the demo on github and it works in my version of IE11 om my Win 10 64 machine. I do some more tests on my client machines.

One thing I remember reading when searching for solutions is that some mentioned, the reason is in whether the PDF files is opened inside IE or IE is opening Adobe Acrobat Reader. Opening it in IE should be the cause and that is consistent with EDGE where it is opened in the reader. So somewhere in IE or AAR it should be possible to decide how pdf files are opened in IE.

I'll see if I can find out.

Regards
Soren
Reply
#9
Hi All,

I have now found a solution to the IE11 problem of not showing the PDF, but instead a blank screen, often blocking the browser for a while or completely. I do not know whether this is the only solution (I suspect not), and though it is working, it is not working quite as I would want. Like it does in Edge.

The solution lies in IE11 and the Manage of Add-Ons, and partly in IE11 security:

In IE11, open tools and select Add Ons.

If the list of Add-Ons, Toolbars and Extentions contains Adobe PDF Reader then, according to the support of Adobe, Disabling the Add On, should be sufficient to make IE11 open the PDF file outside IE11, using Adobe Reader. When it is active IE11 will try to open it inside IE11.

If it is not listed, then further down, in the drop down box where it says: currently loaded add-ons, select "run without permission". If Adobe PDF Reader is in the list, then leave it active, leave Manage Add-Ons and go back to Tools, and select ActiveX-Filtering to activate it.

That way it works in my machine (WIN 10 64-1903) with IE11.

AND if you have similar issues with other browsers, then Adobe has a very fine description of how to make it work in most browsers:

In the Start menu (on windows), select "Adobe Reader DC" to open the reader without a specific PDF file. Select Edit and Settings, and select the menu point Internet on the left. In the right side pane that shows, it a link line saying "how to set up your browser to use adobe products to view pdf documents". That points to an Adobe site showing how-to for most available browsers.

What I meant with not working quite as I would want, is the way the PDF is shown. In IE11, at least the way it's set up on my machine, it uses a full page (fit to page size) rather than showing it with the actual sizes. I use it to show receipts created as PDF files. They are 80 mm wide and typically 160 mm long, and in EDGE, Chrome And Firefox, they are show as realistic receipts, in the middle of the screen, with gray area around, leaving an impression of actually looking at a copy of the receipt, whereas IE11 fits the 80mm width to the entire screen, with the result that you do not see the actual receipt, but the text it contains, in a very large font. I prefer it the way the other browsers do it.

It might just be a setting somewhere i IE11, but I won't be using more time on it. However, if you have a solution to that, please feel free to share it, and I will try it.

Regards
Soren
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)