GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 27, 2014 6:56 pm

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: question Regarding the poppler tutorial
PostPosted: Tue Jan 24, 2012 8:53 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
alright, so ive examined the simple pdf reader caracal posted here viewtopic.php?t=3421 so my question is how can i modify the file url string in poppler_document_new_from_file into "file/user/test.pdf" instead of the argument value array?(argv[1]) ive been trying to do so but i am getting a simple EXECUTING: message in console and i can see no window.


Regards


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Thu Jan 26, 2012 12:56 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

poppler_document_new_from_file() takes a simple string as a mandatory parameter. You just need to make sure your file name is passed in as a URI (the form of URI should be file:///path/to/your/file.pdf).

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Fri Jan 27, 2012 3:03 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
thanks for your help i did replace the argv array with the string but when i run the program it exits itself automatically!(i dont provide the code beacause its the same one used in the tutorial with only change the uri of the parameter)


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Sun Jan 29, 2012 9:17 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
I dont know whats wrong but now im getting segmatation fault......


heres what i try to do

Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <poppler.h>

static PopplerDocument* doc;
static PopplerPage* page;
static cairo_t* cr;

static void
on_destroy(GtkWidget* w, gpointer data) {
    gtk_main_quit();
}

int main(int argc, char* argv[]) {
    GtkWidget* win;
   GtkWidget *vbox;
   GError* err = NULL;
   

   gtk_init(&argc, &argv);
   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   vbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(win, vbox);

     doc = poppler_document_new_from_file("file:/home/user/Downloads/pdftest.pdf", NULL, &err);
   if (!doc) {
        printf("%s\n", err->message);
        g_object_unref(err);
        return 2;
    }
    page = poppler_document_get_page(doc, 0);
    if(!page) {
        printf("Could not open first page of document\n");
        g_object_unref(doc);
        return 3;
    }
       cr = gdk_cairo_create(win->window);
    poppler_page_render(page, cr);
    cairo_destroy(cr);
   
   gtk_box_pack_start(vbox, cr, TRUE ,TRUE, 0);
   
    g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
    gtk_widget_set_app_paintable(win, TRUE);
    gtk_widget_show_all(win);

    gtk_main();

    return 0;
}


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Jan 30, 2012 6:15 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

You're getting segfault because win->window doesn't exist yet when you call gdk_cairo_create(). Also, drawing onto widgets is only allowed from expose-event callback (or draw callback if you're using GTK+-3.x). I tested sample code posted by caracal, replaced argv[1] with static string and things worked as expected. Why are you changing the general layout of the app before you even managed to get the app working with your custom PDF file?

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Jan 30, 2012 7:38 am 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
thanks for your answer im replacing argv[1] with the string but when i run it the program exits itself automatically.Im using ubuntu 11.10


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Jan 30, 2012 9:08 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

In your code I see this:
Code:
doc = poppler_document_new_from_file("file:/home/user/Downloads/pdftest.pdf", NULL, &err);


file:/home/user/Downloads/pdftest.pdf is not valid URI according to the standard. You should use file:///home/user/Downloads/pdftest.pdf. If things still fail after that, then either a) your pdf file doesn't exist or b) Ubuntu does something strange with this GTK+ app.

I tested this on my Gentoo x86_64 box with latest stable GTK+-2 (I think it's 2.24, not 100% sure).

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Sun Feb 12, 2012 3:51 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
i am using anjuta to make this,what about you?what libraries must be included?


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 2:59 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

This is code excerpt from original tutorial post:
Code:
/* gcc `pkg-config --cflags --libs gtk+-2.0 poppler-glib` -o pdfviewer pdfviewer.c */

You you need gtk+-2.0 and poppler-glib. Not sure what's troubling you, since if libraries were not present, this app wouldn't even compile or at least throw some warnings when compiling.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:01 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
just to let you know,im getting Program exited successfully with errcode(1)


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:16 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.
qwertyui wrote:
just to let you know,im getting Program exited successfully with errcode(1)

If your code is structured as original one, this means that not enough arguments are passed to the application. If you modified sources not to require arguments, then you should have removed that check.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:20 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
thanks for your rapid answer,its appreciated :)

What do you mean with the arguments?I dont understand why,i have only modified the arg[1] array with the adress of the pdf file

Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <poppler.h>

/* gcc `pkg-config --cflags --libs gtk+-2.0 poppler-glib` -o pdfviewer pdfviewer.c */

static PopplerDocument* doc;
static PopplerPage* page;

static void
on_destroy(GtkWidget* w, gpointer data) {
    gtk_main_quit();
}

static gboolean
on_expose(GtkWidget* w, GdkEventExpose* e, gpointer data) {
    cairo_t* cr;
    cr = gdk_cairo_create(w->window);
    poppler_page_render(page, cr);
    cairo_destroy(cr);
    return FALSE;
}

int main(int argc, char* argv[]) {
    GtkWidget* win;
    GError* err = NULL;

    if (argc != 2) {
        printf("Useage: pdfviewer <uri>\n");
        return 1;
    }

    gtk_init(&argc, &argv);

    doc = poppler_document_new_from_file("file:///home/user/test.pdf", NULL, &err);
    if (!doc) {
        printf("%s\n", err->message);
        g_object_unref(err);
        return 2;
    }

    page = poppler_document_get_page(doc, 0);
    if(!page) {
        printf("Could not open first page of document\n");
        g_object_unref(doc);
        return 3;
    }

    int pages = poppler_document_get_n_pages(doc);
    printf("There are %d pages in this pdf.\n", pages);
     
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(win), "destroy",      G_CALLBACK(on_destroy), NULL);
    g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(on_expose), NULL);
    gtk_widget_set_app_paintable(win, TRUE);
    gtk_widget_show_all(win);

    gtk_main();

    g_object_unref(page);
    g_object_unref(doc);

    return 0;
}


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:26 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I commented in code what is wrong.

Code:
int main(int argc, char* argv[]) {
    GtkWidget* win;
    GError* err = NULL;

    /* ********************************************************************
       Next test checks that application is called with exactly 1 argument.
       If no argument is passed, it returns 1.

       Since you don't use argv array at all, you should remove this test.
     * ****************************************************************** */
    if (argc != 2) {
        printf("Useage: pdfviewer <uri>\n");
        return 1;
    }

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:33 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
Damn you are correct!!!
I didnt even notice that part :)
However im getting segmatation fault now :/


Top
 Profile  
 
 Post subject: Re: question Regarding the poppler tutorial
PostPosted: Mon Feb 13, 2012 3:41 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

First, make sure that /home/user/test.pdf exists. If file is there, you'll probably need to run your app under gdb to get a backtrace.

Cheers,
Tadej


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group