mirror of
https://github.com/michaelrsweet/pdfio.git
synced 2025-10-08 19:55:04 +02:00
141 lines
4.3 KiB
C
141 lines
4.3 KiB
C
// File: my_pdfa_test.c (Final version with embedded font)
|
|
|
|
#include "pdfio.h"
|
|
#include "pdfio-content.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
//
|
|
// Local functions...
|
|
//
|
|
static bool my_error_cb(pdfio_file_t *pdf, const char *message, void *data);
|
|
static bool create_pdfa_test_file(const char *filename, const char *pdfa_version);
|
|
static int test_pdfa(void);
|
|
|
|
|
|
//
|
|
// 'my_error_cb()' - A simple error callback to print detailed messages.
|
|
//
|
|
static bool // O - true to continue, false to stop
|
|
my_error_cb(pdfio_file_t *pdf, // I - PDF file
|
|
const char *message, // I - Error message
|
|
void *data) // I - Callback data (unused)
|
|
{
|
|
(void)pdf; // Unused
|
|
(void)data; // Unused
|
|
fprintf(stderr, "PDFio Error: %s\n", message);
|
|
return (false); // Stop on any error
|
|
}
|
|
|
|
|
|
//
|
|
// 'create_pdfa_test_file()' - A helper function to generate a simple PDF/A file.
|
|
//
|
|
static bool // O - true on success, false on error
|
|
create_pdfa_test_file(
|
|
const char *filename, // I - Name of the PDF file to create
|
|
const char *pdfa_version) // I - PDF/A version string (e.g., "PDF/A-1b")
|
|
{
|
|
pdfio_file_t *pdf; // Output PDF file
|
|
pdfio_rect_t media_box = { 0.0, 0.0, 612.0, 792.0 };
|
|
// Media box for US Letter
|
|
pdfio_obj_t *font; // Font object
|
|
pdfio_dict_t *page_dict; // Page dictionary
|
|
pdfio_stream_t *st; // Page content stream
|
|
char text[256]; // Text to write to page
|
|
|
|
snprintf(text, sizeof(text), "This is a compliance test for %s.", pdfa_version);
|
|
printf(" Creating '%s' for %s compliance check...\n", filename, pdfa_version);
|
|
|
|
if ((pdf = pdfioFileCreate(filename, pdfa_version, &media_box, NULL, my_error_cb, NULL)) == NULL)
|
|
{
|
|
fprintf(stderr, " ERROR: pdfioFileCreate failed for '%s'.\n", filename);
|
|
return (false);
|
|
}
|
|
|
|
// --- THIS IS THE KEY CHANGE ---
|
|
// Instead of using a base font, we now load and embed a font from a file.
|
|
if ((font = pdfioFileCreateFontObjFromFile(pdf, "testfiles/OpenSans-Regular.ttf", false)) == NULL)
|
|
{
|
|
fprintf(stderr, " ERROR: Unable to embed font 'testfiles/OpenSans-Regular.ttf'.\n");
|
|
pdfioFileClose(pdf);
|
|
return (false);
|
|
}
|
|
|
|
page_dict = pdfioDictCreate(pdf);
|
|
pdfioPageDictAddFont(page_dict, "F1", font);
|
|
st = pdfioFileCreatePage(pdf, page_dict);
|
|
|
|
pdfioContentSetTextFont(st, "F1", 12.0);
|
|
pdfioContentTextBegin(st);
|
|
pdfioContentTextMoveTo(st, 72.0, 720.0);
|
|
pdfioContentTextShow(st, false, text);
|
|
pdfioContentTextEnd(st);
|
|
|
|
pdfioStreamClose(st);
|
|
pdfioFileClose(pdf);
|
|
|
|
printf(" Successfully created '%s'.\n", filename);
|
|
return (true);
|
|
}
|
|
|
|
|
|
//
|
|
// 'test_pdfa()' - The main test runner for the PDF/A feature.
|
|
//
|
|
static int // O - 0 on success, 1 on error
|
|
test_pdfa(void)
|
|
{
|
|
int status = 0;
|
|
pdfio_file_t *fail_pdf;
|
|
pdfio_rect_t media_box = { 0.0, 0.0, 612.0, 792.0 };
|
|
|
|
puts("---- Running PDF/A Generation Tests ----\n");
|
|
|
|
if (!create_pdfa_test_file("test-pdfa-1b.pdf", "PDF/A-1b")) status = 1;
|
|
if (!create_pdfa_test_file("test-pdfa-2b.pdf", "PDF/A-2b")) status = 1;
|
|
if (!create_pdfa_test_file("test-pdfa-2u.pdf", "PDF/A-2u")) status = 1;
|
|
if (!create_pdfa_test_file("test-pdfa-3b.pdf", "PDF/A-3b")) status = 1;
|
|
if (!create_pdfa_test_file("test-pdfa-3u.pdf", "PDF/A-3u")) status = 1;
|
|
if (!create_pdfa_test_file("test-pdfa-4.pdf", "PDF/A-4")) status = 1;
|
|
|
|
puts("\n---- Running PDF/A Encryption Block Test ----\n");
|
|
|
|
printf(" Creating PDF/A file to test encryption failure...\n");
|
|
if ((fail_pdf = pdfioFileCreate("test-pdfa-fail.pdf", "PDF/A-1b", &media_box, NULL, my_error_cb, NULL)) == NULL)
|
|
{
|
|
fprintf(stderr, " ERROR: pdfioFileCreate failed for encryption test.\n");
|
|
return (1);
|
|
}
|
|
|
|
if (pdfioFileSetPermissions(fail_pdf, PDFIO_PERMISSION_ALL, PDFIO_ENCRYPTION_RC4_128, "owner", "user"))
|
|
{
|
|
fputs(" ERROR: pdfioFileSetPermissions succeeded but should have FAILED!\n", stderr);
|
|
status = 1;
|
|
}
|
|
else
|
|
{
|
|
puts(" SUCCESS: Correctly blocked encryption for PDF/A file as expected.");
|
|
}
|
|
pdfioFileClose(fail_pdf);
|
|
|
|
puts("\n-------------------------------------------");
|
|
if (status == 0)
|
|
puts("✅ All PDF/A tests passed.");
|
|
else
|
|
puts("❌ One or more PDF/A tests FAILED.");
|
|
puts("-------------------------------------------\n");
|
|
|
|
return (status);
|
|
}
|
|
|
|
|
|
//
|
|
// 'main()' - Main entry for the test program.
|
|
//
|
|
int // O - Exit status
|
|
main(void)
|
|
{
|
|
return (test_pdfa());
|
|
}
|