I am trying to build a code that converts txt file to pdf file.
I managed to create the pdf file. The problem is the pdf file is created corrupted. I would like to know whatīs wrong with the code. Below I put the complete code. I hope someone help me.
Code:
#include <cairo.h>
#include <cairo-pdf.h>
#include<stdio.h>
#include<conio.h>
#include<conio2.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main(int argc, char **argv) {
float position;
char str[200];
FILE *fp;
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_pdf_surface_create("Inventário.pdf", 504, 648);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, 5.0);
position=50.0;
fp = fopen("arquivo.txt", "r");
if(!fp) return 1;
while(fgets(str,sizeof(str),fp) != NULL)
{
int len = strlen(str)-1;
if(str[len] == '\n') str[len] = 0;
printf("\x0a %s", str);
cairo_move_to(cr, 8.0, position);
cairo_show_text(cr, str);
position+=8;
}
fclose(fp);
getch();
cairo_show_page(cr);
cairo_surface_destroy(surface);
cairo_destroy(cr);
return 0;
}