Thread: CAIRO - error when converting txt to pdf

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    8

    CAIRO - error when converting txt to pdf

    I am trying to build a code that converts txt file to pdf file.

    I managed to create the pdf file. The problem is the pdf file is created corrupted. I would like to know whatīs wrong with the code. Below I put the complete code. I hope someone help me.

    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    #include<stdio.h>
    #include<conio.h>
    #include<conio2.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    int main(int argc, char **argv) {    
            
     float position;
       char str[200];
      FILE *fp;
      cairo_surface_t *surface;
      cairo_t *cr;
    
      surface = cairo_pdf_surface_create("Inventário.pdf", 504, 648);
    
      cr = cairo_create(surface);
    
      cairo_set_source_rgb(cr, 0, 0, 0);
    
      cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
    
      cairo_set_font_size (cr, 5.0);
    
      position=50.0;
    
      fp = fopen("arquivo.txt", "r");
        if(!fp) return 1; 
      while(fgets(str,sizeof(str),fp) != NULL) 
      {
          int len = strlen(str)-1;
          if(str[len] == '\n') str[len] = 0;
          printf("\x0a %s", str);
          cairo_move_to(cr, 8.0, position);
          cairo_show_text(cr, str);
    
      position+=8;
     }
      fclose(fp);
       getch();
      cairo_show_page(cr);
    
      cairo_surface_destroy(surface);
      cairo_destroy(cr);
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The problem is the pdf file is created corrupted.
    As in "Adobe acrobat viewer pops up an error message saying "This is an invalid PDF file"" ?

    Or

    Adobe acrobat opens the file OK, but all you see is some combination of
    - all white
    - all black
    - random ink blots
    - lines
    - something else

    First off, I would suggest you ditch the file reading, and just go with
    Code:
      position=50.0;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      position+=8;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 2");
      cairo_show_page(cr);
    Then fiddle around with the font size, position etc until you understand how to display 2 lines at a nice scale and position.
    Cairo: A Vector Graphics Library

    Then do the code for reading the file.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by Salem View Post
    > The problem is the pdf file is created corrupted.
    As in "Adobe acrobat viewer pops up an error message saying "This is an invalid PDF file"" ?

    Or

    Adobe acrobat opens the file OK, but all you see is some combination of
    - all white
    - all black
    - random ink blots
    - lines
    - something else

    First off, I would suggest you ditch the file reading, and just go with
    Code:
      position=50.0;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      position+=8;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 2");
      cairo_show_page(cr);
    Then fiddle around with the font size, position etc until you understand how to display 2 lines at a nice scale and position.
    Cairo: A Vector Graphics Library

    Then do the code for reading the file.
    Thanks for the answer.
    I did what you told me. Look the code.
    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    //#include<stdio.h>
    //#include<conio.h>
    //#include<conio2.h>
    //#include <stdlib.h>
    //#include <string.h>
    //#include <windows.h>
    int main() {
        
        float position;
     
      cairo_surface_t *surface;
      cairo_t *cr;
      surface = cairo_pdf_surface_create("Inventario.pdf", 504, 648);
      cr = cairo_create(surface);
      position=50.0;
      
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      position+=8;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 2");
      
      cairo_show_page(cr);
      
      cairo_surface_destroy(surface);
      
      cairo_destroy(cr);
    }
    It is still happening the same problem.
    The pdf file is created corruped and the program does not execute normaly, once it appers this error message = process returned -1073741819 (0x0000005). When I use the GCC, it happers the error Process terminated with status -1073741819 (27910 minutes, 23 seconds).

    I wrote the code as you told me so that I learn first how to white lines in pdf files. Is there any problem with it? Why is happing these erros. I am sure that the corruped pdf files are cause by these errors.

    I would like to remember you that there is no error when I compile. The error happen when I excute. So, spite of this error, the pdf file is created, but is created with error too, as you already know.

    Please, help me.
    Last edited by leonardoadoado; 12-03-2012 at 08:59 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> It is still happening the same problem.
    So does that mean the weird status value has been the problem all along? That does not mean that the PDF is corrupt.

    >> Process terminated with status -1073741819 (27910 minutes, 23 seconds).
    This can happen if the program terminates in a weird code path.

    If this is happening all the time, you need to step through your program line by line and figure out what is actually happening.
    Last edited by whiteflags; 12-03-2012 at 09:04 PM. Reason: actually C++'s main() does not need a return statement, duh

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    >> It is still happening the same problem.
    So does that mean the weird status value has been the problem all along? That does not mean that the PDF is corrupt.

    >> Process terminated with status -1073741819 (27910 minutes, 23 seconds).
    This can happen if the program terminates in a weird code path.

    If this is happening all the time, you need to step through your program line by line and figure out what is actually happening.
    Look this code...

    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    //#include<stdio.h>
    //#include<conio.h>
    //#include<conio2.h>
    //#include <stdlib.h>
    //#include <string.h>
    //#include <windows.h>
    int main() {
        
        float position;
     
      cairo_surface_t *surface;
      cairo_t *cr;
      surface = cairo_pdf_surface_create("Inventario.pdf", 504, 648);
      cr = cairo_create(surface);
      position=50.0;
      
      I did not put that part you told me.
      
      cairo_show_page(cr);
      
      cairo_surface_destroy(surface);
      
      cairo_destroy(cr);
    }
    If I do not put anything in the pdf file, just create it, compilation is fine and when I execute it, there is no problem and it is created one empty page of pdf file with no error and nothing writen, but it is not corruped.

    So, I think the problem is when I use the comands cairo_move_to(cr, 8.0, position); and
    cairo_show_text(cr, "This is line 1"); or maybe is happing some problem when the datas are put on the pdf file, once the exe and the pdf file run rightly when I just create with nothing writen.

    I hope you help me to solve this problem.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    True or false - you can load the resulting PDF into adobe acrobat, and you see "something", even if it just a mess of pixels.

    > once it appers this error message = process returned -1073741819 (0x0000005).
    This just means you returned garbage from main.

    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    //#include<stdio.h>
    //#include<conio.h>
    //#include<conio2.h>
    //#include <stdlib.h>
    //#include <string.h>
    //#include <windows.h>
    int main() {
        
        float position;
     
      cairo_surface_t *surface;
      cairo_t *cr;
      surface = cairo_pdf_surface_create("Inventario.pdf", 504, 648);
      cr = cairo_create(surface);
      position=50.0;
      
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      position+=8;
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 2");
      
      cairo_show_page(cr);
      
      cairo_surface_destroy(surface);
      
      cairo_destroy(cr);
    
      //!! yes, really!
      return 0;
    }
    If you make the last thing in your main a return 0, then you should see consistent success.
    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.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Usually when you run the clean up (destroy) functions don't you typically do so in opposite order to the create functions? This is how I've seen things in my years of programming. Of course I know nothing of this particular library so I could be wrong but I see this:

    Code:
    cairo_surface_t *surface;
    cairo_t *cr;
    surface = cairo_pdf_surface_create("Inventario.pdf", 504, 648);
    cr = cairo_create(surface);
    ...where you create the surface and the use the returned pointer to call the other create function. Then I see this:
    Code:
    cairo_surface_destroy(surface);
       
    cairo_destroy(cr);
    ...and can't help but think the two lines should be swapped so the surface gets destroyed last. Again, maybe I'm wrong but everything I've worked with like this kind of thing does it the way I'm suggesting. As an example, using Oracle Call Interface to set up a database connection we call a create environment function first followed be a create connection function call:
    Code:
    oracle::occi::Connection * conn = NULL;
    oracle::occi::Environment * env = oracle::occi::Environment::createEnvironment();
    
    ...
    
    conn = env->createConnection(strUser,strPass,strConn);
    ...then we call these corresponding terminate functions in reverse order, starting with the connection and then the environment:
    Code:
    env->terminateConnection(conn);
    oracle::occi::Environment::terminateEnvironment(env);
    ...maybe I'm wrong, but the way it's being done doesn't "feel" right.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Looks like your copy of cairo is crashing. Guys, look at the error code. The value -1073741819 is 0xC0000005 in hex, which is the Windows status code equivalent to "segmentation fault."

    Failing to return a value from main() is not the cause of a corrupted PDF file. hk_mp5kpdw's comment might be the answer.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by brewbuck View Post
    Looks like your copy of cairo is crashing. Guys, look at the error code. The value -1073741819 is 0xC0000005 in hex, which is the Windows status code equivalent to "segmentation fault."

    Failing to return a value from main() is not the cause of a corrupted PDF file. hk_mp5kpdw's comment might be the answer.
    Of course it is!
    I just looked at the hex part and assumed the OP had copied it correctly. Manually converting the large negative number shows the truth!
    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.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by Salem View Post
    Of course it is!
    I just looked at the hex part and assumed the OP had copied it correctly. Manually converting the large negative number shows the truth!
    So, the problem is the library cairo?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I think the consensus is that post #7 is a good possibility.

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by Matticus View Post
    I think the consensus is that post #7 is a good possibility.
    Maybe #7 is correct. But, Donīt you think that if He is right, this code should work? Donīt you agree?

    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    
    int main() {
        
        float position;
     
      cairo_surface_t *surface;
      cairo_t *cr;
      surface = cairo_pdf_surface_create("file.pdf", 504, 648);
      cr = cairo_create(surface);
      position=50.0;
      
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      
      cairo_show_page(cr);
      
      cairo_surface_destroy(surface);
      
      cairo_destroy(cr);
    }
    }
    I think that in this code, I do not need to clean the memony of the pointer more than once. Once I am trying to write just one line. So, Donīt you think that according to #7, this code should work?
    Last edited by leonardoadoado; 12-04-2012 at 04:14 PM.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by leonardoadoado View Post
    Maybe #7 is correct. But, Donīt you think that if He is right, this code should work? Donīt you agree?
    No -- the point of post #7 is to exchange the order of lines 19 and 21.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    8
    Quote Originally Posted by brewbuck View Post
    No -- the point of post #7 is to exchange the order of lines 19 and 21.
    I exchanged the order of lines 19 and 21. The error is still the same

    Code:
    #include <cairo.h>
    #include <cairo-pdf.h>
    #include<conio.h>
    int main() {
        
        float position;
     
      cairo_surface_t *surface;
      cairo_t *cr;
      surface = cairo_pdf_surface_create("Inventario.pdf", 504, 648);
      cr = cairo_create(surface);
      position=50.0;
      
      cairo_move_to(cr, 8.0, position);
      cairo_show_text(cr, "This is line 1");
      
      cairo_show_page(cr); getch();
      
      cairo_destroy(cr);
      
      cairo_surface_destroy(surface);
      
    }
    I think that in this case, The order is ok.

    I put a getch(); after the command cairo_show_text, and the error bengins before this getch(), once the error happen before executing the getch();

    if I put this getch() before the command cairo_show_text, the getch() is executed this time, and after this, the error happens.
    The conclution is that something is causing an error in the command cairo_show_page.

    Did you understand my point?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I just downloaded libcairo2 onto my Ubuntu system, tried post #14 and it works just as expected. I get a valid PDF file with the text clearly visible.
    At this point, I'd say there is something wrong with the library you installed, or something in the way you're building the code.

    What is your actual programming environment? You've mentioned windows.h and that you're using gcc.

    If you can, do a "build->clean" and a "build->rebuild all"
    Then post your build log.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cairo-dock issue
    By Annonymous in forum Linux Programming
    Replies: 2
    Last Post: 06-03-2012, 12:14 AM
  2. Using image as a mask in cairo (a c library)
    By javaeyes in forum C Programming
    Replies: 3
    Last Post: 03-21-2012, 05:09 AM
  3. cairo, rasterizing text
    By javaeyes in forum C Programming
    Replies: 1
    Last Post: 03-06-2012, 08:51 PM
  4. cairo text extent weirdness
    By javaeyes in forum C Programming
    Replies: 3
    Last Post: 02-24-2012, 07:24 PM
  5. Cairo and GTK, still need a pixmap?
    By TriKri in forum Linux Programming
    Replies: 0
    Last Post: 03-18-2008, 04:18 PM