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

Carl Worth cworth at freedesktop.org
Fri Jul 31 01:59:15 PDT 2009


 src/renderpdf.mdwn |  115 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 110 insertions(+), 5 deletions(-)

New commits:
commit ffde9e89e01fbfee85768e13554e87c6c9815bb9
Author: ajohnson <ajohnson at web>
Date:   Fri Jul 31 01:59:13 2009 -0700

    Add PDF to image example

diff --git a/src/renderpdf.mdwn b/src/renderpdf.mdwn
index 7d80e3b..790daf9 100644
--- a/src/renderpdf.mdwn
+++ b/src/renderpdf.mdwn
@@ -4,11 +4,12 @@ PDF files can be rendered to a cairo context using poppler. PS or EPS files can
 
 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:
+Compile the examples with:
 
-    gcc -o pdf2cairo pdf2cairo.c `pkg-config --cflags --libs cairo poppler-glib`
+    gcc -o pdftops pdftops.c `pkg-config --cflags --libs cairo poppler-glib`
+    gcc -o pdftoimage pdftoimage.c `pkg-config --cflags --libs cairo poppler-glib`
 
-pdf2cairo.c:
+pdftops.c - Render a PDF file to a cairo PostScript surface.
 
     #include <poppler.h>
     #include <cairo.h>
@@ -72,8 +73,6 @@ pdf2cairo.c:
             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);
@@ -93,3 +92,109 @@ pdf2cairo.c:
 
         return 0;
     }
+
+
+pdftoimage.c - Render a one page of a PDF file to a cairo image surface.
+
+    #include <stdio.h>
+    #include <stdlib.h>
+    #include <poppler.h>
+    #include <cairo.h>
+
+    #define IMAGE_DPI 150
+
+    int main(int argc, char *argv[])
+    {
+        PopplerDocument *document;
+        PopplerPage *page;
+        double width, height;
+        GError *error;
+        const char *pdf_file;
+        const char *png_file;
+        gchar *absolute, *uri;
+        int page_num, num_pages;
+        cairo_surface_t *surface;
+        cairo_t *cr;
+        cairo_status_t status;
+
+        if (argc != 4) {
+            printf ("Usage: pdftoimage input_file.pdf output_file.png page\n");
+            return 0;
+        }
+
+        pdf_file = argv[1];
+        png_file = argv[2];
+        page_num = atoi(argv[3]);
+        g_type_init ();
+        error = NULL;
+
+        if (g_path_is_absolute(pdf_file)) {
+            absolute = g_strdup (pdf_file);
+        } else {
+            gchar *dir = g_get_current_dir ();
+            absolute = g_build_filename (dir, pdf_file, (gchar *) 0);
+            free (dir);
+        }
+
+        uri = g_filename_to_uri (absolute, NULL, &error);
+        free (absolute);
+        if (uri == NULL) {
+            printf("%s\n", error->message);
+            return 1;
+        }
+
+        document = poppler_document_new_from_file (uri, NULL, &error);
+        if (document == NULL) {
+            printf("%s\n", error->message);
+            return 1;
+        }
+
+        num_pages = poppler_document_get_n_pages (document);
+        if (page_num < 1 || page_num > num_pages) {
+            printf("page must be between 1 and %d\n", num_pages);
+            return 1;
+        }
+
+        page = poppler_document_get_page (document, page_num - 1);
+        if (page == NULL) {
+            printf("poppler fail: page not found\n");
+            return 1;
+        }
+
+        poppler_page_get_size (page, &width, &height);
+
+        /* For correct rendering of PDF, the PDF is first rendered to a
+         * transparent image (all alpha = 0). */
+        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                              IMAGE_DPI*width/72.0,
+                                              IMAGE_DPI*height/72.0);
+        cr = cairo_create (surface);
+        cairo_scale (cr, IMAGE_DPI/72.0, IMAGE_DPI/72.0);
+        cairo_save (cr);
+        poppler_page_render (page, cr);
+        cairo_restore (cr);
+        g_object_unref (page);
+
+        /* Then the image is painted on top of a white "page". Instead of
+         * creating a second image, painting it white, then painting the
+         * PDF image over it we can use the CAIRO_OPERATOR_DEST_OVER
+         * operator to achieve the same effect with the one image. */
+        cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
+        cairo_set_source_rgb (cr, 1, 1, 1);
+        cairo_paint (cr);
+    
+        status = cairo_status(cr);
+        if (status)
+            printf("%s\n", cairo_status_to_string (status));
+
+        cairo_destroy (cr);
+        status = cairo_surface_write_to_png (surface, png_file);
+        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