Thread: Print pdf to network printer

  1. #1
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Print pdf to network printer

    Hi guys,

    Looking for some help again... I've searched the forums and I've come as far as this:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdafx.h>
    
    int main(void)
    {
      char ih;
      FILE *fp=fopen("lpt4", "w"), *fpin=fopen("test.pdf", "rb");
      char flin[256];
    
      if ( fp == NULL )
      {
        perror( "No printer" );
        getch();
        exit( 1 );
      }
    	
      while((ih=fgetc(fpin))!=EOF)
      {
        fputc(ih,fp);
        putch(ih);
      }
      getch();
      return 0;
    }
    with the examples I've found. This bit of code doesnt print anything though. It works fine for a text file (because the fputc bit sends every single character to the printer) but it doesnt work for a pdf or other non-text file...
    Does anyone have an idea of what to do to make this work?

    I've also tried
    Code:
    fprintf(fp, "test.pdf");
    But that results in a nice A4 paper with the text string "test.pdf" on it


    Hope you can help, thanks a lot in advance!

    Ren&#233;
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    - Alternatively, is it possible to execute a batch-like line:
    type test.pdf >lpt4
    from a C program?


    - Second alternative: I could create a batch file with that line and use
    Code:
    system("tralala.bat");
    But the filename to print should be variable.... is there a possibility to pass a variable to a batch file?




    EDIT: I could of course create a batch file like this:
    Code:
    *fp=fopen("tralala.bat", "w");
    fprintf(fp,"type %s >lpt4", pdf_file);
    and then execute it with the system line above... this would be a solution, but of course the best would be if I needn't use a batch file!

    Thanks for any help
    Last edited by rkooij; 10-31-2006 at 09:18 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Have you tried opening the printer as a binary file, like you open your PDF file?

  4. #4
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by SKeane
    Have you tried opening the printer as a binary file, like you open your PDF file?
    Hadn't tried that before no. Just did it, and it doesn't work either It prints some weird characters on the screen and the printer just doesnt respond.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,316
    > but it doesnt work for a pdf or other non-text file...
    I'm assuming you do get a whole bunch of gibberish which begins with "%PDF".

    Printers have many different control languages in addition to understanding plain text. PDF isn't one of them.

    Postscript on the other hand is.

    So at the very least you need to read the PDF file, figure out what all those bits and bytes mean, create some kind of page "layout" of each page in the PDF document, then send the result to the printer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    >> but it doesnt work for a pdf or other non-text file...
    >I'm assuming you do get a whole bunch of gibberish which begins with "%PDF".
    yep

    >So at the very least you need to read the PDF file, figure out what all those bits and bytes mean, create some kind of page "layout" of each page in the PDF document, then send the result to the printer.
    Hmm.. how about: "no thanks"? :P

    I've implemented the batch "strategy" and that works perfectly! Thanks for your help though!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The batch actually works? I'm astonished.

    Err, but I'm sure you implementation is too complicated. This would suffice:
    Code:
    #define CMD_TEMPLATE "type %s > lpt4"
    char *cmd = malloc(strlen(CMD_TEMPLATE) + strlen(pdf_file) + 1);
    sprintf(cmd, CMD_TEMPLATE, pdf_file);
    system(cmd);
    free(cmd);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by CornedBee
    The batch actually works? I'm astonished.

    Err, but I'm sure you implementation is too complicated. This would suffice:
    Code:
    #define CMD_TEMPLATE "type %s > lpt4"
    char *cmd = malloc(strlen(CMD_TEMPLATE) + strlen(pdf_file) + 1);
    sprintf(cmd, CMD_TEMPLATE, pdf_file);
    system(cmd);
    free(cmd);
    Nice idea, the template defining..! I actually didn't even know %s was allowed and working in defines! What about the free(cmd); by the way? Does system(); automatically request memory?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I actually didn't even know %s was allowed and working in defines!
    It's just a string. It doesn't "work" in defines. It's the *printf family of functions that make it work.

    The free() is to counter the malloc() I have earlier.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    1ST R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by CornedBee
    It's just a string. It doesn't "work" in defines. It's the *printf family of functions that make it work.
    Yeah, that I understood

    Quote Originally Posted by CornedBee
    The free() is to counter the malloc() I have earlier.
    D'oh, missed that malloc then!


    Thanks
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A free TIFF to PDF library
    By rockytriton in forum C++ Programming
    Replies: 0
    Last Post: 01-06-2006, 03:22 PM
  2. Printer output mirrored!
    By Bajanine in forum Windows Programming
    Replies: 1
    Last Post: 11-27-2004, 05:48 PM
  3. printing on network printer
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2001, 02:10 PM
  4. Print to the printer
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-18-2001, 10:22 AM
  5. How to print out the data throw printer.Attn Salem
    By Jason in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 05:58 AM