Finish QPDFJob examples and add tests for them

This commit is contained in:
Jay Berkenbilt 2022-02-01 09:58:32 -05:00
parent b02d37bc0a
commit a0d9d9923c
11 changed files with 368 additions and 12 deletions

View File

@ -17,7 +17,8 @@ BINS_examples = \
qpdf-job
CBINS_examples = \
pdf-c-objects \
pdf-linearize
pdf-linearize \
qpdfjob-c
TARGETS_examples = $(foreach B,$(BINS_examples) $(CBINS_examples),examples/$(OUTPUT_DIR)/$(call binname,$(B)))

View File

@ -1,20 +1,27 @@
#include <qpdf/QPDFJob.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>
#include <iostream>
#include <cstring>
// QXXXQ describe
// This program is a simple demonstration of different ways to use the
// QPDFJob API.
static char const* whoami = 0;
#if 0 // QXXXQ
static void usage(std::string const& msg)
static void usage()
{
std::cerr << "Usage: " << whoami << " QXXXQ" << std::endl;
std::cerr
<< "Usage: " << whoami << std::endl
<< "This program linearizes the first page of in.pdf to out1.pdf,"
<< " out2.pdf, and"
<< std::endl
<< " out3.pdf, each demonstrating a different way to use the"
<< " QPDFJob API"
<< std::endl;
exit(2);
}
#endif
int main(int argc, char* argv[])
{
@ -26,24 +33,90 @@ int main(int argc, char* argv[])
whoami += 3;
}
if (argc != 1)
{
usage();
}
// The examples below all catch std::exception. Note that
// QPDFUsage can be caught separately to report on errors in using
// the API itself. For CLI, this is command-line usage. For JSON
// or the API, it would be errors from the equivalent invocation.
// Note that staticId is used for testing only.
try
{
// Use the config API
QPDFJob j;
j.config()
->inputFile("/tmp/1.pdf")
->outputFile("/tmp/2.pdf")
->inputFile("in.pdf")
->outputFile("out1.pdf")
->pages()
->pageSpec(".", "1-z")
->pageSpec(".", "1")
->endPages()
->qdf()
->linearize()
->staticId() // for testing only
->checkConfiguration();
j.run();
std::cout << "out1 status: " << j.getExitCode() << std::endl;
}
catch (std::exception& e)
{
// QXXXQ catch usage, configerror, whatever we end up with separately
std::cerr << "exception: " << e.what() << std::endl;
return 2;
}
try
{
char const* new_argv[] = {
whoami,
"in.pdf",
"out2.pdf",
"--linearize",
"--pages",
".",
"1",
"--",
"--static-id",
nullptr
};
QPDFJob j;
j.initializeFromArgv(9, new_argv);
j.run();
std::cout << "out2 status: " << j.getExitCode() << std::endl;
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << std::endl;
return 2;
}
try
{
// Use the JSON API
QPDFJob j;
j.initializeFromJson(R"({
"inputFile": "in.pdf",
"outputFile": "out3.pdf",
"staticId": "",
"linearize": "",
"pages": [
{
"file": ".",
"range": "1"
}
]
}
)");
j.run();
std::cout << "out3 status: " << j.getExitCode() << std::endl;
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << std::endl;
return 2;
}
return 0;
}

62
examples/qpdfjob-c.c Normal file
View File

@ -0,0 +1,62 @@
/*
* This is an example program to linearize a PDF file using the C
* QPDFJob API.
*/
#include <qpdf/qpdfjob-c.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char const* whoami = 0;
static void usage()
{
fprintf(stderr, "Usage: %s infile outfile\n", whoami);
exit(2);
}
int main(int argc, char* argv[])
{
char* infile = NULL;
char* outfile = NULL;
char const* new_argv[6];
int r = 0;
char* p = 0;
if ((p = strrchr(argv[0], '/')) != NULL)
{
whoami = p + 1;
}
else if ((p = strrchr(argv[0], '\\')) != NULL)
{
whoami = p + 1;
}
else
{
whoami = argv[0];
}
if (argc != 3)
{
usage();
}
infile = argv[1];
outfile = argv[2];
new_argv[0] = "qpdfjob";
new_argv[1] = infile;
new_argv[2] = outfile;
new_argv[3] = "--linearize";
new_argv[4] = "--static-id"; /* for testing only */
new_argv[5] = NULL;
/* See qpdf-job.cc for a C++ example of using the json interface.
* To use that from C just like the argv one, call
* qpdfjob_run_from_json instead and pass the json string as a
* single char const* argument.
*/
r = qpdfjob_run_from_argv(5, new_argv);
return r;
}

View File

@ -0,0 +1,32 @@
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
chdir("qpdf-job") or die "chdir testdir failed: $!\n";
require TestDriver;
cleanup();
my $td = new TestDriver('qpdf-job');
$td->runtest("qpdf-job",
{$td->COMMAND => "qpdf-job"},
{$td->FILE => "out", $td->EXIT_STATUS => 0},
$td->NORMALIZE_NEWLINES);
for (my $i = 1; $i <= 3; ++$i)
{
$td->runtest("check output $i",
{$td->FILE => "out$i.pdf"},
{$td->FILE => "out.pdf"});
}
cleanup();
$td->report(4);
sub cleanup
{
unlink "out1.pdf", "out2.pdf", "out3.pdf";
}

View File

@ -0,0 +1,78 @@
%PDF-1.3
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids [
3 0 R
]
/Count 1
>>
endobj
3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources <<
/ProcSet 5 0 R
/Font <<
/F1 6 0 R
>>
>>
>>
endobj
4 0 obj
<<
/Length 44
>>
stream
BT
/F1 24 Tf
72 720 Td
(Potato) Tj
ET
endstream
endobj
5 0 obj
[
/PDF
/Text
]
endobj
6 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000063 00000 n
0000000135 00000 n
0000000307 00000 n
0000000403 00000 n
0000000438 00000 n
trailer <<
/Size 7
/Root 1 0 R
>>
startxref
544
%%EOF

View File

@ -0,0 +1,3 @@
out1 status: 0
out2 status: 0
out3 status: 0

Binary file not shown.

View File

@ -0,0 +1,29 @@
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
chdir("qpdf-job") or die "chdir testdir failed: $!\n";
require TestDriver;
cleanup();
my $td = new TestDriver('qpdfjob-c');
$td->runtest("qpdfjob-c",
{$td->COMMAND => "qpdfjob-c in.pdf a.pdf"},
{$td->STRING => "", $td->EXIT_STATUS => 0},
$td->NORMALIZE_NEWLINES);
$td->runtest("check output",
{$td->FILE => "a.pdf"},
{$td->FILE => "out.pdf"});
cleanup();
$td->report(2);
sub cleanup
{
unlink "a.pdf";
}

View File

@ -0,0 +1,78 @@
%PDF-1.3
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids [
3 0 R
]
/Count 1
>>
endobj
3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources <<
/ProcSet 5 0 R
/Font <<
/F1 6 0 R
>>
>>
>>
endobj
4 0 obj
<<
/Length 44
>>
stream
BT
/F1 24 Tf
72 720 Td
(Potato) Tj
ET
endstream
endobj
5 0 obj
[
/PDF
/Text
]
endobj
6 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000063 00000 n
0000000135 00000 n
0000000307 00000 n
0000000403 00000 n
0000000438 00000 n
trailer <<
/Size 7
/Root 1 0 R
>>
startxref
544
%%EOF

Binary file not shown.

View File

@ -39,7 +39,7 @@
/*
* This file provides a minimal wrapper around QPDFJob. See
* examples/qpdf-job.c for an example of its use.
* examples/qpdfjob-c.c for an example of its use.
*/
#ifdef __cplusplus