Print Report from Webservice (No PDF)

deV.chdeV.ch Member Posts: 543
edited 2012-01-17 in NAV Three Tier
I tried to print a report (showrequestform=false, showprinterdialog=false) but this is not possible from WS. I get an error saing "Client callback (such as showing Pages) is not supported when executing custom Web services". Why is this not possible? I don't have a page or any user interaction! The reason for this is we want to replace a NAS Solution with a Webservice.
«1

Answers

  • mihail_kolevmihail_kolev Member Posts: 379
    Just you can't do it. My workaround was to make external application and a Scheduled Task to print PDFs.
    -Mihail- [MCTS]
  • deV.chdeV.ch Member Posts: 543
    Ok i found a solution that prints quick enough for my requirements:

    -I generate the PDF with SAVEASPDF
    -I use R2's DotNet Interop to use a DLL from Forensictools.net that can print PDF without the need of Acrobat Reader (But Printer must support PostScript)
    The DLL is free! http://forensictools.net/

    That way you can print your document to a printer of your choice in no time.
  • deV.chdeV.ch Member Posts: 543
    And to complete it, i found a solution for every type of printer. I now use Foxit Reader (a free pdf reader) to print my files, the reader is extremly performant and just a single exe file is needed to work.

    I use .NET interop to start a process with the parameter -t <pdf file> <printer> to print the generated pdf file. Printing time is very fast (like 2 sec. reaction time from webservice call to printing start).
  • ara3nara3n Member Posts: 9,256
    I use foxit as well. I haven't touched adobe software for a while. except shockwave but html 5 will replace that eventually.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • nightrodnightrod Member Posts: 68
    Hi guys i have tried this solution with saveas pdf and then print the doc using foxit.
    in my C# program it gives me this error:
    {"This message is for C/AL programmers: The call to member Run failed: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)."}

    i cant solve this ](*,) .
    anyone got some ideas?

    is there a more simple solution to print through webservice yet? i am using nav 2009 R2.

    Thanx!
  • ara3nara3n Member Posts: 9,256
    you are probably running code

    Report.run

    instead you need to change it to Report.saveasPDF.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • nightrodnightrod Member Posts: 68
    Hi Rashed,

    The saveaspdf part works fine.
    its the printing part that gives the problem.
    The shell foxit does not work.
    i used the code from this post:
    viewtopic.php?f=23&t=27688&start=0&hilit=foxit


    Thanks, Johan
  • ara3nara3n Member Posts: 9,256
    you'll have to wait for deV.ch to post detail of his method.

    My guess the user running the webservice doesn't have permission.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • deV.chdeV.ch Member Posts: 543
    I do it with DotNet Interop:

    PrintPDF Function:
    PrintPDF(_FileName : Text[250];_Printer : Text[100])
    PrintDoc := PrintDoc.PrintDocument();
    DefaultPrinter := PrintDoc.PrinterSettings.PrinterName;
    IF _Printer = '' THEN
      _Printer := DefaultPrinter;
    
    FoxitReader := 'C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe';
    StartProcess(FoxitReader,STRSUBSTNO('-t "%1" "%2"',_FileName,_Printer));
    

    StartProcess Function:
    StartProcess(_Filename : Text[250];_Arguments : Text[250])
    Process := Process.Process();
    ProcessInfo := ProcessInfo.ProcessStartInfo(_Filename,_Arguments);
    Process.StartInfo := ProcessInfo;
    Process.Start();
    Process.WaitForExit(10000); // Wait max. 10 sec
    

    Name DataType Subtype Length
    Process DotNet 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process
    ProcessInfo DotNet 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.ProcessStartInfo
  • nightrodnightrod Member Posts: 68
    Hi dev.ch,

    there did came a paper out of the printer \:D/ .
    But still got an error:

    This message is for C/AL programmers: The call to member Run failed: The system cannot find the file specified. (Exception from HRESULT: 0x80070002).


    the program prints the document and can continue.
    any idea?

    Thanks a lot!!!
  • deV.chdeV.ch Member Posts: 543
    Please post all your code... do you run the report after you call SAVEASPDF?
  • nightrodnightrod Member Posts: 68
    Dont know why but it runs without errors now.
    i am very happy with it.
    Thanks a lot =D>

    This is the code.
    the function startrprocess is exactly like above.
    filename := 'c:\NAVDOCS\invoice.pdf';
    Command := 'c:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe';
    printer := 'Brother MFC-9840CDW Printer';
    ReportNr := 50000;
    
    IF FILE.EXISTS(filename) THEN
     ERASE(filename);
    
    
    SalesHeader.SETRANGE(SalesHeader."Document Type",SalesHeader."Document Type"::Invoice);
    SalesHeader.SETRANGE(SalesHeader."No.",headerNo);
    
    IF SalesHeader.FINDFIRST THEN;
      REPORT.SAVEASPDF(ReportNr,filename,SalesHeader);
    
    WHILE NOT FILE.EXISTS(filename) AND (TimeOut < 10) DO BEGIN
          SLEEP(10000);
          TimeOut := TimeOut + 1;
          IF TimeOut >9 THEN;
    END;
    
    IF FILE.EXISTS(filename) THEN
      StartProcess(Command,STRSUBSTNO('-t "%1" "%2"',filename,printer));
    
  • deV.chdeV.ch Member Posts: 543
    Is this code:
      REPORT.SAVEASPDF(ReportNr,filename,SalesHeader);
    
    WHILE NOT FILE.EXISTS(filename) AND (TimeOut < 10) DO BEGIN
          SLEEP(10000);
          TimeOut := TimeOut + 1;
          IF TimeOut >9 THEN;
    END;
    

    realy needed? I thought SAVEASPDF exits as soon as the file is generated so that such a check would be useless. have you tested this?
  • nightrodnightrod Member Posts: 68
    i am not sure if its needed.
    when generating files with for example bullzip pdf printer i use it to.
    so when the next comando is givven i am sure the file exists.
    it works anyway.
  • sbillysbilly Member Posts: 231
    nightrod wrote:
    Dont know why but it runs without errors now.
    i am very happy with it.
    Thanks a lot =D>

    This is the code.
    the function startrprocess is exactly like above.
    filename := 'c:\NAVDOCS\invoice.pdf';
    Command := 'c:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe';
    printer := 'Brother MFC-9840CDW Printer';
    ReportNr := 50000;
    
    IF FILE.EXISTS(filename) THEN
     ERASE(filename);
    
    
    SalesHeader.SETRANGE(SalesHeader."Document Type",SalesHeader."Document Type"::Invoice);
    SalesHeader.SETRANGE(SalesHeader."No.",headerNo);
    
    IF SalesHeader.FINDFIRST THEN;
      REPORT.SAVEASPDF(ReportNr,filename,SalesHeader);
    
    WHILE NOT FILE.EXISTS(filename) AND (TimeOut < 10) DO BEGIN
          SLEEP(10000);
          TimeOut := TimeOut + 1;
          IF TimeOut >9 THEN;
    END;
    
    IF FILE.EXISTS(filename) THEN
      StartProcess(Command,STRSUBSTNO('-t "%1" "%2"',filename,printer));
    
    Please can U tell me what's the "headerNo" ??
  • nightrodnightrod Member Posts: 68
    The function is called from a C# webapplication.
    headerNo is the parameter to get the right invoice.
    (the invoice number i send from C#).
  • sbillysbilly Member Posts: 231
    nightrod wrote:
    The function is called from a C# webapplication.
    headerNo is the parameter to get the right invoice.
    (the invoice number i send from C#).
    thanks a lot, I understand now, I want to ask u about startprocess function, what's the data type of "process" and how can I set it>
  • nightrodnightrod Member Posts: 68
    Proces is datatype: Dotnet with subtype:
    'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process

    ProcesInfo is datatype Dotnet with subtype:
    'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b

    The StartProces function is this:
    Process := Process.Process();
    ProcessInfo := ProcessInfo.ProcessStartInfo(_Filename,_Arguments);
    Process.StartInfo := ProcessInfo;
    Process.Start();
    Process.WaitForExit(10000); // Wait max. 10 sec77a5c561934e089'.System.Diagnostics.ProcessStartInfo
    
  • sbillysbilly Member Posts: 231
    I didn't find "dotnet" in datatype.
    Is there a dll I must add?
  • nightrodnightrod Member Posts: 68
    That i dont know.
    i think you need to.
  • sbillysbilly Member Posts: 231
    I am using NAVISION 2009 SP1.
    Tell me, when u add a global variable , u have "dotnet " in datatype list?
    Please can u export ur codeunit that include the startprocess function.
  • deV.chdeV.ch Member Posts: 543
    Of course u need Nav2009 R2 for this, the DataType DotNet ist only available in R2!

    if you want to do this in SP1 you need to use Windows Script Host Shell.
  • sbillysbilly Member Posts: 231
    I wrote this line
    SHELL("C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe /p /h C:\PdfDocuments\bb.pdf");
    
    But I have a warning.
    This's what u mean by script shell or u have another solution.
  • deV.chdeV.ch Member Posts: 543
    No the SHELL command is not supported anymore. you need the automation of type: 'Windows Script Host Object Model'.WshShell

    with this you can run the command. Don't forget to create the automation:
    if isclear(WSH) then
      create(WSH,true);
    
  • sbillysbilly Member Posts: 231
    wooooooooooow thanks very much u saved me
    Please u know the commande that print directly form foxit, like in adobe one ""C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe /p /h C:\PdfDocuments\bb.pdf""
    And thank u one more time
  • deV.chdeV.ch Member Posts: 543
    In this thread on page 1 i've posted the syntax ;)

    it is '<PathToFoxit.exe> -t <PathToPDFfile> <Printer>'
  • sbillysbilly Member Posts: 231
    deV.ch wrote:
    In this thread on page 1 i've posted the syntax ;)

    it is '<PathToFoxit.exe> -t <PathToPDFfile> <Printer>'
    hihiih
    I do wsh.run or wsh.exec
    please can u give me the right code
    and sorry :oops:
  • deV.chdeV.ch Member Posts: 543
    wsh.Run('C:\program files\foxit\foxitreader.exe -t c:\temp\mypdf.pdf myprinter');
  • sbillysbilly Member Posts: 231
    when i try it , it give me an error
    An exception was raised in methode Run. The OLE control or automation server has returned error (HRESULT) -2147352567
  • deV.chdeV.ch Member Posts: 543
    then try Exec
Sign In or Register to comment.