PDFCreator AutoSave issue.

nvermanverma Member Posts: 396
I have created a table where the user can go in and input a name of the report as well as the e-mail address of the user the report should be sent too and the system will automatically run the report and save it in a PDF format and send it to a particular e-mail address. To do this, I created a code-unit and use the PDFCreator functionality. Keep in mind that codeunit will be executed using NAS.
One of the main issue that people have faced when using NAS and PDFCreator is "Window.Open" dialog box (which I have commented out in order to avoid that problem). Everything works fine except every once in a while, the system throws an error message: "Auto-save mode: The destination file J:\plm\max users.pdf is in use. The process was stopped to prevent data loss. " To prevent this error, after the report is run, I used SLEEP(30000) before executing SMTP code. This does not work all the time. Any ideas how I can bypass this problem.
IF ISCLEAR(PDFCreator) THEN
  CREATE(PDFCreator);
IF ISCLEAR(PDFCreatorError) THEN
  CREATE(PDFCreatorError);

IF CreatePDF.FIND('-') THEN BEGIN
REPEAT
  ReportName := CreatePDF."Report Name";
 
  Object.SETFILTER(Object.Type, '%1', Object.Type::Report);
  Object.SETFILTER(Object.Name, ReportName);
  Object.FIND('-');
  ReportID := Object.ID;

  IF Object.GET(Object.Type::Report,'',ReportID) THEN;
  FileDirectory := 'J:\PLM\';
  FileName := ReportName +'.pdf';
 
  PDFCreatorError := PDFCreator.cError;

  IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
       ERROR('Status: Error[' + FORMAT(PDFCreatorError.Number) + ']: ' + PDFCreatorError.Description);

 // Window.OPEN('processing');
 // WindowisOpen := TRUE;
  IF FileName = '' THEN
    ERROR('Please specify what the file should be saved as');

  Object.GET(Object.Type::Report,'',ReportID);

  PDFCreatorOption :=  PDFCreator.cOptions;
  PDFCreatorOption.UseAutosave := 1;
  PDFCreatorOption.UseAutosaveDirectory := 1;
  PDFCreatorOption.AutosaveDirectory := FileDirectory;
  PDFCreatorOption.AutosaveFormat := 0;                       //PDF file, you can also save in other formats
  PDFCreatorOption.AutosaveFilename := FileName;

  PDFCreator.cOptions := PDFCreatorOption;
  PDFCreator.cClearCache();
  DefaultPrinter := PDFCreator.cDefaultPrinter;
  PDFCreator.cDefaultPrinter := 'PDFCreator';
  PDFCreator.cPrinterStop := FALSE;

  IF FILE.EXISTS('J:\PLM\FileName') THEN
    FILE.ERASE('J:\PLM\FileName');

  REPORT.RUNMODAL(ReportID,FALSE,TRUE);
  // Window.CLOSE;
  SLEEP(30000);

  Subject := FileName;
  Body := 'Please find the enclosed copy of "' + ReportName + '" report.';
  SMTPMail.CreateMessage(SenderName,SenderAddress, CreatePDF."E-Mail", Subject , Body, TRUE );
  SMTPMail.AddAttachment('J:\PLM\' + FileName);
  SMTPMail.Send();

  UNTIL CreatePDF.NEXT =0;
END;

CLEAR(PDFCreator);
CLEAR(PDFCreatorOption);
CLEAR(PDFCreatorError);

Normally, when a report is run, the user would generally filter the report by a particular Cost Center Code. I was trying to give the user a similar functionality. Therefore in my table "CreatePDF" (where the user will input the name of the report, and e-mail address) I created a field called Cost Center Code, where the user will be able to input the cost center code that he/she wishes to filter upon. I am just not sure how I can implement the filtering of report by the cost center that the user entered in the CreatePDF table.

Any idea or suggestions that I could try out. Any help will be much appreciated.


NOTE: I have tried doing this to check if the pdf is created. After REPORT.RUNMODAL
REPEAT
UNTIL PDFCreator.cIsConverted;

It just froze NAV. ](*,)

Answers

  • ara3nara3n Member Posts: 9,256
    write a while loop where you can check and see that the is created and to see if it's finished try and change the date and time on the pdf file. If pdfcreator is still working you won't be able to change the date and time on it so that way you know that it's still working.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • nvermanverma Member Posts: 396
    does pdfcreateor have a function to change the time or to modify the date of the pdf???

    I tried looking for it...i couldnt find anything

    In the mean time I tried doing this:
    Ok:=  TestFile.OPEN('J:\PLM\'+FileName);
      Count :=0;
     // TestFile.WRITEMODE := TRUE;
      WHILE (NOT Ok) AND (Count <100)  DO BEGIN //THEN
        SLEEP(1000);
        Count := Count +1;
        Ok:=  TestFile.OPEN('J:\PLM\'+FileName);
      END;
      TestFile.CLOSE;
    

    I wrote that code after Report.runmodal(ReportID,FALSE,TRUE);
    AND
    before Subject :=FileName;

    Some of the files that the users generate are like 1000+ pages, so this wont work in that case. ](*,)

    anyone else had this issue, regarding the Auto-Save Mode error. The PDFCreator Version I am using is 1.5.1.

    I also tried this line of code after the report.runmodal (my efforts were futile, it still gave me the auto-save error):
    WHILE (NOT File.SETSTAMP('J:\PLM\'+FileName,TODAY,TIME)) DO
    BEGIN
    SLEEP(1000);
    END;
    
  • nvermanverma Member Posts: 396
    SOLVED!!! - Credit goes to NAGI....
    OnRun()
    //MODCNV01 - ADD START
    IF ISCLEAR(PDFCreator) THEN
      CREATE(PDFCreator);
    IF ISCLEAR(PDFCreatorError) THEN
      CREATE(PDFCreatorError);
    
    //Runs the function below
    Okay:=Code();
    
    //Run the PrintPDF codeunit to send the printed reports to pre-define e-mail address
    IF Okay THEN
      PrintingPDF.RUN();
    //MODCNV01 - ADD END
    
    Code() OK : Boolean
    //MODCNV01 - ADD START
    //Go through all the entries in the in the CreatePDF table and for each entry create a PDF copy of the reports.
    IF CreatePDF.FIND('-') THEN BEGIN
    REPEAT
      Done := FALSE;
      ReportID:= CreatePDF."Object ID";
    
      Object.SETFILTER(Object.Type, '%1', CreatePDF."Object Type");
      Object.SETFILTER(Object.ID, '%1', CreatePDF."Object ID");
      Object.FIND('-');
      ReportName := Object.Name;
    
      IF Object.GET(Object.Type::Report,'',ReportID) THEN;
      FileDirectory := 'J:\PLM\';
      FileName := ReportName +'.pdf';
     
      PDFCreatorError := PDFCreator.cError;
      IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
           ERROR('Status: Error[' + FORMAT(PDFCreatorError.Number) + ']: ' + PDFCreatorError.Description);
    
      Object.GET(Object.Type::Report,'',ReportID);
    
      PDFCreatorOption :=  PDFCreator.cOptions;
      PDFCreatorOption.UseAutosave := 1;
      PDFCreatorOption.UseAutosaveDirectory := 1;
      PDFCreatorOption.AutosaveDirectory := FileDirectory;
      PDFCreatorOption.AutosaveFormat := 0;                       //PDF file, you can also save in other formats
      PDFCreatorOption.AutosaveFilename := FileName;
    
      PDFCreator.cOptions := PDFCreatorOption;
      PDFCreator.cClearCache();
      DefaultPrinter := PDFCreator.cDefaultPrinter;
      PDFCreator.cDefaultPrinter := 'PDFCreator';
      PDFCreator.cPrinterStop := FALSE;
    
      SnoozeTime := 3000;
      ExitLoop := FALSE;
    // While ExitLoop is false, run the report. After 3 seconds, check to see if the file exists. If it does, exit out of the loop. If
    // it doesnt exist, put the system to sleep and go through the while loop again until the file exists.
      WHILE NOT ExitLoop DO BEGIN
        REPORT.RUNMODAL(ReportID,FALSE,TRUE);
        SLEEP(SnoozeTime);
        IF EXISTS(FileDirectory + FileName) THEN
          ExitLoop := TRUE
        ELSE
          SnoozeTime += 1000;
    
    // To avoid getting stuck in the infinite loop, this exist condition has been put in place.
        IF SnoozeTime >=10000 THEN
          ExitLoop := TRUE;
      END;     
    
      UNTIL CreatePDF.NEXT =0;
    END;
    
    CLEAR(PDFCreator);
    CLEAR(PDFCreatorOption);
    CLEAR(PDFCreatorError);
    OK:=TRUE;
    //MODCNV01 - ADD END
    
    PDFCreator::eReady()
    Done:=TRUE;
    
Sign In or Register to comment.