Angus March wrote:
I tried using Poppler to get a Cairo surface and then saving the surface
to a PNG. Unfortunately, the resulting image was of disastrously low
quality.

Without seeing your code or the output you are getting I can only guess at what the problem might be. Did you alter the cairo scale to get the desired image dpi?

The attached code works for me.

#include <stdio.h>
#include <stdlib.h>
#include <poppler.h>
#include <cairo.h>

#define IMAGE_DPI 300

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("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);
    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);
    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);
    poppler_page_render (page, cr);
    g_object_unref (page);

    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;
}
_______________________________________________
poppler mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/poppler

Reply via email to