简体   繁体   中英

CGI not printing under MAMP

I'm using MAMP (so on Mac) and working on CGI in C. I've made the following code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("Content-Type: text/html; charset=utf-8\n\n");
    printf("Hello <b> in bold</b>\n");
    FILE *f = popen("lp -", "w");
    if (!f)
    {
       printf("<b>ERROR</b>\n");    
       exit(1);
    }
   fprintf(f, "This will be send to printer");
   return 0;
}

When I run it under Terminal (after compiling of course), I see the text "Hello in bold" and the output of "This will be send to printer" is correctlty send to the printer. But when I put the CGI in cgi-bin folder in MAMP and call the CGI, the text is correctly displayed (meaning the CGI is correctly executed), but there is no output to the printer. Why? Any idea?

It works! First thanks a lot to those who had put coments, helping a lot. I'll explain what I've done as it can be useful outside of this CGI problem. Running the CGI (code in the question) from the desktop of the Mac was OK. Running as CGI give no printer output but the text "Hello in bold " was correctly displayed on the web page. Having a look at the Apache Erro log (tail on Applications/MAMP/logs/apache_error.log), thanks to Antti Haapala, I see:

 lp\xc2\xa0: erreur - aucune destination par d\xc3\xa9faut disponible.

Meaning "No default destination avaible". After performing "which lp" (thanks to I'L'I) I get /usr/bin/lp, set it in my popen() call and run the CGI. And I get in the error log:

 /usr/bin/lp\xc2\xa0: erreur - aucune destination par d\xc3\xa9faut disponible.

So same answer... After searching I find a strange point: when I run the CGI from the desktop (so clicking on it), it prints, using my default printer (EPSON M105) which I set in the Preferences panel of my Mac. Meaning when you use popen(lp) from the desktop, it works. But when you use it in a CGI throught Apache, for the CGI there is no default printer, meaning Mac OSX seems to have two default printer setting. So, under Terminal, I've done:

   lpstat -a

to get the list of the printer available. Then, just do:

   lpoptions -d PRINTERNAME

replacing "PRINTERNAME" by the name of the printer you get from the prievious comande, to set the default printer for "lp".

Then run again the CGI. Now, it prints but.... give an Internal Server Error!!

After seaching again, I find that using lp with "w" output the name of the printer. Strangly, its seems the ouput is done while performing the pclose() as if you don't put the pclose() you don't have the Error 500. To use the pclose() and avoid this ouput , you must use "r+" in popen() rather than "w".

The good code is:

 #include <stdio.h>
 #include <stdlib.h>

 int main(void)
 {
     int status;

    printf("Content-Type: text/html; charset=utf-8\n\n");
    printf("Hello test 7  <b> in bold</b>\n");

    FILE *f = popen("lp -", "r+");

    if (!f)
     {
        printf("<b>ERROR</b>\n");   
        exit(1);
     }

    fprintf(f, "Text that will be send to the printer");
    fflush(f);
    status = pclose(f);

    printf("Hello <b> in bold 2</b>\n");
    return 0;
 }

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM