Options

Print report to pdf through code

finderfinder Member Posts: 129
Hi,

I found a lot of topics which explain how to print report to pdf but didn't find exact solution for my idea. I want to print report to pdf through code using freeware software. At the moment I started with solution where in first step I'll save report as html then using htmldoc program (which is freeware and allows run it from command line with different parameters) converting it to pdf. As htmldoc program has some problems with blank textboxes (after converting shows symbols &nbsp) I also had to run program that deletes these symbols before converting. The whole process together is a little bit clumsy (save as html then remove &nbsp symbols with external program then converting to pdf with external program - htmldoc) and I want to improve it.
Can somebody suggest some programs for printing reports directly to pdf which are freeware and have included ocx or automation server.

Thanks in advance,
Anti

Comments

  • Options
    kinekine Member Posts: 12,562
    1) I am using the PDFCreator now, because there is Automation interface for it in the newest version. You can set properties through this interface...
    2) Saving report into PDF has 3 main problems:
    a) You need to say to the PDF printer where you want to save the PDF and the filename of the PDF
    b) You need to select the PDF printer for the report you want to print
    c) You need to check if the PDF is created or not to continue with your code (and to be able to attach the PDF to mail etc.)

    a) The automation solve that, you can turn on the auto-save and set the directory and filename in PDFCreator
    b) You can change the CU1 function for selecting printer to return PDFCreator printer when you are printing into PDF through code (you just needs to pass the parameter into CU1 - see howto's for how to pass parameters between objects) - in this case you do not need to set default printer to PDFCreator or set the printer for the report in Printer selection table.
    c) PDFCreator automation has one Event which is fired when printing was finished (and second when error was called). But you needs to find solution how to wait for that trigger and not block NAV by some loop, else the trigger will be not fired... (I am using modal form with timer checking some flag which is set by the trigger)

    I am not allowed to post you the code or example of it, but this is the general description what you need to solve.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ara3nara3n Member Posts: 9,255
    Hello Here is a howto example for creating pdf files from navision reports.

    http://www.mibuso.com/forum/viewtopic.php?t=18331&highlight=
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    finderfinder Member Posts: 129
    Thanks kine and ara3n!
    I've got PDFCreator testversion for Vista and were now able to test this solution. As kine said there are three main problems to solve in saving report to pdf. I used the example code: http://www.mibuso.com/forum/viewtopic.p ... highlight= but didn't manage to save file automatically. Maybe the problem is in PDFCreator Vista version. Finally I changed PDFCreator autosave options in settings file and solved the problem this way. Printer selection wasn't the problem but then I stucked on last problem. I used trigger PDFCreator::eReady() to close PDFCreator and change back to default printer. But sometimes there is a problem with closing PDFCreator and then I get an error "An instance of PDFCreator is already running". CLEAR(PFCreator) clears variable but when PDFCreator monitor is still running it gives me such an error message. I tried to put sleep-function to wait some time before starting with next report but how should I know how long to wait (it can vary a lot). Do you have any suggestions how to solve it.

    Thanks in advance,
    Anti
  • Options
    finderfinder Member Posts: 129
    Now I tried the same code in Windows Server 2003 and these file saving options worked correctly (in 0.9.3 version) and that one is solved. But I had the same problem with closing PDFCreator.exe (an error message that one instance is already running). It looks like the command PDFCreator.cClose cannot always close the program. Has somebody seen the same problem or got an idea how to solve it.
    I really appreciate your help!
  • Options
    ara3nara3n Member Posts: 9,255
    I would post it on pdfcreator forum and see if you get an answer from them.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    finderfinder Member Posts: 129
    I found similar topics also in pdfcreator forum. It seems that sometimes this pdfcreator process hangs and stays alive in process table. Fortunately there is an additional parameter for PDFCreator.cStart ForceInit and if to set it TRUE then cStart doesn't give an error if there is already one instance of PDFCreator running. I hope it won't cause any other trouble.
  • Options
    ara3nara3n Member Posts: 9,255
    Thanks for the info. I've updated the tips and tricks document.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    teckpohteckpoh Member Posts: 271
    Hi All,

    I'm using pdfCreator to generate pdf file, i'm facing strange circumstance where sometimes the coding work well but sometimes the pdf file can't save automatically although i already specify the filePath & fileName.
    When the time it can't save automatically pdfCreator prompt me a screen ask me to enter keyword, subject, author... This was really annoying...

    Do any 1 hv tis kind of experience b4? Any solution???

    Below is my code:

    IF ISCLEAR(PDFCreator) THEN
    CREATE(PDFCreator);
    IF ISCLEAR(PDFCreatorError) THEN
    CREATE(PDFCreatorError);

    filedir := 'C:\sentbox';
    filename := "Sales Invoice Header"."No."+'.pdf';
    PDFCreatorError := PDFCreator.cError;

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

    PDFCreatorOption := PDFCreator.cOptions;


    PDFCreatorOption.AutosaveDirectory := filedir;
    PDFCreatorOption.AutosaveFilename := filename;
    PDFCreatorOption.UseAutosave := 1;
    PDFCreatorOption.UseAutosaveDirectory := 1;
    PDFCreatorOption.AutosaveFormat := 0;


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

    mycust.RESET;
    mycust.SETRANGE(mycust."No.","Sales Invoice Header"."Sell-to Customer No.");
    IF mycust.FIND('-') THEN BEGIN
    IF mycust."E-Mail" <> '' THEN BEGIN
    mymail.NewMessage(mycust."E-Mail",'','Sales Invoice Report','Regards, teckpoh',
    'C:\Sentbox\'+"Sales Invoice Header"."No." +'.pdf',
    FALSE);
    mymail.Send();
    END;
    END;
  • Options
    David_SingletonDavid_Singleton Member Posts: 5,479
    teckpoh wrote:
    Hi All,

    I'm using pdfCreator to generate pdf file, i'm facing strange circumstance where sometimes the coding work well but sometimes the pdf file can't save automatically although i already specify the filePath & fileName.
    When the time it can't save automatically pdfCreator prompt me a screen ask me to enter keyword, subject, author... This was really annoying...

    Do any 1 hv tis kind of experience b4? Any solution???...

    I had the exact same problem. I uninstalled PDFCreator, reinstalled and now it works fine.
    David Singleton
  • Options
    finderfinder Member Posts: 129
    I think I had the same problem when I tested PDFCreator with Navision and if I remember correctly then I found an easiest solution to configure path and filename already in PDFCreator. There is setting as Auto-Save where you can configure to save automatically and also which path and filename to use.
  • Options
    teckpohteckpoh Member Posts: 271
    Hi All,

    I used PdfCreator to send an email and my problem is inside the email body how can i write a sentence that split into 2 row.
    I used the code below:

    IF mycust.FIND('-') THEN BEGIN
    IF mycust."E-Mail" <> '' THEN BEGIN
    mymail.NewMessage(mycust."E-Mail",'','Sales Invoice Report','Regards, +'\teckpoh',
    mymail.Send();

    but the result is
    Regards, teckpoh

    instead of :
    Regard,
    teckpoh

    Any advise...? Thank in advance~~
  • Options
    finderfinder Member Posts: 129
    As I undestand you used CU 397 for sending e-mail but I don't have an experience with this. I have used SmtpMailX OCX (look here in download section) for sending e-mail and it worked well for me. There you are able to add many different lines to e-mail body using method SMTPMail.AddLine('Test');
  • Options
    teckpohteckpoh Member Posts: 271
    Hi finder/All,

    Finally i solved it by using crlf function. Store the email body to array and in between the arrays break a new line with cr & lf
  • Options
    teckpohteckpoh Member Posts: 271
    teckpoh wrote:
    Hi All,

    I'm using pdfCreator to generate pdf file, i'm facing strange circumstance where sometimes the coding work well but sometimes the pdf file can't save automatically although i already specify the filePath & fileName.
    When the time it can't save automatically pdfCreator prompt me a screen ask me to enter keyword, subject, author... This was really annoying...

    Do any 1 hv tis kind of experience b4? Any solution???...

    I had the exact same problem. I uninstalled PDFCreator, reinstalled and now it works fine.

    After uninstall and reinstall the problem still there..most of the time pdfcreator pop up the screen ... :cry: Any 1 ever fix tis kind of problem??
    or anybody is using other useful tool tat act as same as pdfcreator? plz tell me....10s a lot~~
Sign In or Register to comment.