Skip to content

Instantly share code, notes, and snippets.

@roxlu roxlu/main.cpp
Created Feb 22, 2014

Embed
What would you like to do?
LibHARU + PDF
/*
PDF
----
Very basic application which generates a poster from a
set of images. This application was created because I wanted
to have a poster from my instagram images. It uses a couple of
php files that login to my instagram account and generates a
shell script which downloads them to a directory.
Once downloaded all files are saved using a filename like: image-%d.jpg
By setting the "num_images" below I load in all the files and add them to
the PDF.
*/
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <hpdf.h>
#define MM_TO_POINTS(mm) (mm * (72.0/25.4))
void error_handler(HPDF_STATUS errno, HPDF_STATUS detail, void* user) {
printf("Error.\n");
}
int main() {
printf("test.\n");
HPDF_Doc pdf;
HPDF_Page page;
pdf = HPDF_New(error_handler, NULL);
if(!pdf) {
printf("Error: cannot create pdfdoc object.\n");
return EXIT_FAILURE;
}
int num_images = 348;
int img_padding = MM_TO_POINTS(8);
int poster_width = MM_TO_POINTS(841);
int poster_height = MM_TO_POINTS(1189);
int poster_padding = MM_TO_POINTS(70);
int page_width = poster_width - poster_padding;
int page_height = poster_height - poster_padding;
int num_cols = 16;
int img_width = (page_width-(img_padding*num_cols)) / num_cols;
int img_height = img_width;
int num_rows = page_height / img_height;
int max_images = num_cols * num_rows;
while(max_images > num_images) {
num_rows--;
max_images = num_cols * num_rows;
}
// reset/clamp page size
page_width = num_cols * (img_width + img_padding);
page_height = num_rows * (img_height + img_padding);
int offset_x = 0.5 * (poster_width - page_width);
int offset_y = 0.5 * (poster_height - page_height);
printf("Cols per page: %d\n", num_cols);
printf("Rows per page: %d\n", num_rows);
printf("Offset x: %d, offset y: %d\n", offset_x, offset_y);
// create page
page = HPDF_AddPage(pdf);
HPDF_Page_SetHeight(page, poster_height);
HPDF_Page_SetWidth(page, poster_width);
// create background fill
HPDF_Page_SetRGBFill(page, 0, 0, 0);
HPDF_Page_Rectangle(page, 0, 0, poster_width, poster_height);
HPDF_Page_Fill(page);
// add images
char fname[512];
int dx = 0;
for(int j = 0; j < num_rows; ++j) {
for(int i = 0; i < num_cols; ++i) {
if(dx > num_images) {
continue;
}
int x = offset_x + (i * img_width) + i * img_padding;
int y = offset_y + (j * img_height) + j * img_padding;
sprintf(fname, "./downloaded/image-%d.jpg", dx);
printf("Image: %s, %d x %d\n", fname, x, y);
HPDF_Image image;
image = HPDF_LoadJpegImageFromFile(pdf, fname);
HPDF_Page_DrawImage(page, image, x, y, img_width, img_height);
++dx;
}
}
printf("Used: %d images.\n", dx);
HPDF_SaveToFile(pdf, "test.pdf");
HPDF_Free(pdf);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.