[cairo-commit] [cairo-www] src/renderpdf.mdwn

Carl Worth cworth at freedesktop.org
Tue May 13 06:09:21 PDT 2008


 src/renderpdf.mdwn |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

New commits:
commit d4a08a0174196e19ad3c49ac87f230396d7831e2
Author: Carl Worth <cworth at freedesktop.org>
Date:   Tue May 13 06:09:21 2008 -0700

    web commit by ajohnson: Example of using poppler cairo

diff --git a/src/renderpdf.mdwn b/src/renderpdf.mdwn
new file mode 100644
index 0000000..7d80e3b
--- /dev/null
+++ b/src/renderpdf.mdwn
@@ -0,0 +1,95 @@
+##Rendering a PDF or PS file with cairo
+
+PDF files can be rendered to a cairo context using poppler. PS or EPS files can also be rendered to a cairo context by first converting to PDF using Ghostscript.
+
+When using a vector backend, the vectors and text in the PDF file are preserved in the output as vectors. There is no unnecessary rasterization.
+
+Compile the example with:
+
+    gcc -o pdf2cairo pdf2cairo.c `pkg-config --cflags --libs cairo poppler-glib`
+
+pdf2cairo.c:
+
+    #include <poppler.h>
+    #include <cairo.h>
+    #include <cairo-ps.h>
+
+    int main(int argc, char *argv[])
+    {
+        PopplerDocument *document;
+        PopplerPage *page;
+        double width, height;
+        GError *error;
+        const char *filename;
+        gchar *absolute, *uri;
+        int num_pages, i;
+        cairo_surface_t *surface;
+        cairo_t *cr;
+        cairo_status_t status;
+
+        if (argc != 2) {
+            printf ("Usage: pdf2cairo input_file.pdf\n");
+            return 0;
+        }
+
+        filename = argv[1];
+        g_type_init ();
+        error = NULL;
+
+        if (g_path_is_absolute(filename)) {
+            absolute = g_strdup (filename);
+        } else {
+            gchar *dir = g_get_current_dir ();
+            absolute = g_build_filename (dir, filename, (gchar *) 0);
+            free (dir);
+        }
+
+        uri = g_filename_to_uri (absolute, NULL, &error);
+        free (absolute);
+        if (uri == NULL) {
+            printf("poppler fail: %s\n", error->message);
+            return 1;
+        }
+
+        document = poppler_document_new_from_file (uri, NULL, &error);
+        if (document == NULL) {
+            printf("poppler fail: %s\n", error->message);
+            return 1;
+        }
+
+        num_pages = poppler_document_get_n_pages (document);
+
+        /* Page size does not matter here as the size is changed before
+         * each page */
+        surface = cairo_ps_surface_create ("output.ps", 595, 842);
+        cr = cairo_create (surface);
+        for (i = 0; i < num_pages; i++) {
+            page = poppler_document_get_page (document, i);
+            if (page == NULL) {
+                printf("poppler fail: page not found\n");
+                return 1;
+            }
+            poppler_page_get_size (page, &width, &height);
+            cairo_ps_surface_set_size (surface, width, height);
+            cairo_save (cr);
+            /* Use poppler_page_render () for image backends and
+             * poppler_page_render_for_printing() for vector backends. */
+            poppler_page_render_for_printing (page, cr);
+            cairo_restore (cr);
+            cairo_surface_show_page (surface);
+            g_object_unref (page);
+        }
+        status = cairo_status(cr);
+        if (status)
+            printf("%s\n", cairo_status_to_string (status));
+        cairo_destroy (cr);
+        cairo_surface_finish (surface);
+        status = cairo_surface_status(surface);
+        if (status)
+            printf("%s\n", cairo_status_to_string (status));
+        cairo_surface_destroy (surface);
+
+        g_object_unref (document);
+
+        return 0;
+    }


More information about the cairo-commit mailing list