Last active
November 22, 2015 13:14
-
-
Save julian-klode/ac45de6d4d2227d9febc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cairo.h> | |
#include <cairo-pdf.h> | |
#include <poppler.h> | |
#include <stdio.h> | |
int main(int argc, char *argv[]) | |
{ | |
cairo_surface_t *surface; | |
cairo_t *cairo; | |
if (argc < 3) { | |
fprintf(stderr, "Usage: mergepdf INPUT... OUTPUT\n"); | |
return 1; | |
} | |
/* Start our surface with a DIN A4 default. The actual page size will | |
* be set per page, corresponding to the page size of the input page. | |
*/ | |
surface = cairo_pdf_surface_create(argv[argc - 1], 595.276, 841.89); | |
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { | |
fprintf(stderr, "E: Opening %s: %s\n", | |
argv[argc - 1], | |
cairo_status_to_string(cairo_surface_status(surface))); | |
return 1; | |
} | |
cairo = cairo_create(surface); | |
if (cairo_status(cairo) != CAIRO_STATUS_SUCCESS) { | |
fprintf(stderr, "E: Opening %s: %s\n", argv[argc - 1], | |
cairo_status_to_string(cairo_status(cairo))); | |
return 1; | |
} | |
for (int i = 1; i < argc - 1 | |
&& cairo_status(cairo) == CAIRO_STATUS_SUCCESS; i++) { | |
GError *err = NULL; | |
GFile *file; | |
PopplerDocument *doc; | |
int n_pages; | |
file = g_file_new_for_path(argv[i]); | |
doc = poppler_document_new_from_gfile(file, NULL, NULL, &err); | |
if (doc == NULL) { | |
fprintf(stderr, "E: Could not open file %s: %s\n", argv[i], | |
err->message); | |
return 2; | |
} | |
n_pages = poppler_document_get_n_pages(doc); | |
for (int pg = 0; pg < n_pages && | |
cairo_status(cairo) == CAIRO_STATUS_SUCCESS; pg++) { | |
PopplerPage *page; | |
double width; | |
double height; | |
page = poppler_document_get_page(doc, pg); | |
poppler_page_get_size(page, &width, &height); | |
cairo_pdf_surface_set_size(surface, width, height); | |
poppler_page_render_for_printing(page, cairo); | |
cairo_show_page(cairo); | |
g_object_unref(page); | |
} | |
g_object_unref(doc); | |
} | |
if (cairo_status(cairo) != CAIRO_STATUS_SUCCESS) { | |
fprintf(stderr, "E: An error occured while writing: %s\n", | |
cairo_status_to_string(cairo_status(cairo))); | |
return 1; | |
} | |
/* Destroy output objects, so the output file is completed. */ | |
cairo_destroy(cairo); | |
cairo_surface_destroy(surface); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment