Я столкнулся с той же проблемой. В конце концов, я обнаружил, что поступил неправильно, из-за неправильного понимания формата PDF . Мы думаем о файлах PDF как WYSIWYG для принтеров. Это не вариант. При любом виде печати файл PDF преобразуется в некоторый промежуточный формат: PostScript , TIFF изображения или PCL обычно.
Это может произойти на вашем локальном компьютере, поэтому вам нужен драйвер, или на самом принтере. Если это происходит на принтере, вы просто переносите файл PDF на другой компьютер с настроенной системой преобразования.
Это все прекрасно и прекрасно, за исключением того, что PDF не определяет порядок страниц , что очень нелогично для формата, готового к печати. Это означает, что не является первой страницей вашего документа , и вы не сможете определить его каким-либо образом в любом виде.
У вас есть два решения:
Выберите архитектуру принтера и используйте его уникальный метод задания типа носителя, который является болезненным и непереносимым.
Преобразование в формат, который позволяет задавать тип носителя и включает в себя идею упорядочения страниц, например PostScript. Затем добавьте в свои медиа-команды и отправьте этот файл вместе с вашим принтером. Если на вашем принтере есть драйвер для чтения выбранного промежуточного формата, он должен преобразовать команды в свою версию переключения носителей. Это более портативно, но все же не идеально.
Это похоже на идею преобразования вашей C программы в сборку, чтобы перенести ее на новую архитектуру. В основном это работает, но вы должны включить каждую систему в работу.
Гипотетический конвейер будет:
Создайте файл PDF> запустите его с помощью утилиты или библиотеки преобразования PDF в PostScript> запустите пользовательский лексер для добавления команд типов мультимедиа на каждой новой странице> отправьте файл PostScript на принтер
Это большая работа, но это единственное, что вы найдете, чтобы решить вашу проблему.
Пример
%{
char paper[] = 'yourPaper';
%}
%option 8bit outfile="scanner.c"
%option nounput nomain noyywrap
%option warn
%%
showpage { printf("showpage\nsetpagedevice MediaType '%s'", paper); }
%%
int main(int argc, char **argv);
int main (argc,argv)
int argc;
char **argv;
{
yylex();
return 0;
}
Выше приведен простой упрощенный лексер для поиска каждой команды showpage, полученной из stdin, и вывода набора команд showpage setpagedevice. Команда setpagedevice MediaType - это независимый от принтера способ задания типа бумаги, используемой для страницы.
Для компиляции кода используйте flex и GCC :
flex -Cf scanner.l
gcc -O -o lineCount.exe scanner.c
Он принимает ввод через стандартный ввод и выводит в стандартный вывод.
Более полный лексер приведен ниже. Он использует GNU getopts для параметров командной строки и имеет два правила, так что он также устанавливает устройство страницы для первой страницы. Он может не полностью захватить страницы, и у него есть только одна переменная для типа бумаги, поэтому функциональность ограничена. С другой стороны, он очень открыт, но вы хотите, чтобы он определял используемое устройство страницы.
Либо сразу приходят на ум новые правила распознавания того, на какую страницу они смотрят, либо дополнительный входной файл с одной строкой на страницу.
/*
* This file is part of flex.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
/**************************************************
Start of definitions section
***************************************************/
%{
/* A template scanner file to build "scanner.c". */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
/*#include "parser.h" */
//put your variables here
char FileName[256];
FILE *outfile;
char inputName[256];
char paper[] = 'yourPaper';
// flags for command line options
static int specificFile_flag = 0;
static int output_flag = 0;
static int help_flag = 0;
%}
%option 8bit outfile="scanner.c"
%option nounput nomain noyywrap
%option warn
%%
/************************************************
start of rules section
*************************************************/
/* These flex patterns will eat all input */
EndSetup { printf("showpage\nsetpagedevice MediaType '%s'", paper); }
showpage { printf("showpage\nsetpagedevice MediaType '%s'", paper); }
%%
/****************************************************
Start of code section
*****************************************************/
int main(int argc, char **argv);
int main (argc,argv)
int argc;
char **argv;
{
/****************************************************
The main method drives the program. It gets the filename from the
command line, and opens the initial files to write to. Then it calls the lexer.
After the lexer returns, the main method finishes out the report file,
closes all of the open files, and prints out to the command line to let the
user know it is finished.
****************************************************/
int c;
// The GNU getopt library is used to parse the command line for flags
// afterwards, the final option is assumed to be the input file
while (1) {
static struct option long_options[] = {
/* These options set a flag. */
{"help", no_argument, &help_flag, 1},
/* These options don't set a flag. We distinguish them by their indices. */
{"useStdOut", no_argument, 0, 'o'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "o",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c) {
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'o':
output_flag = 1;
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
if (help_flag == 1) {
printf("proper syntax is: traySwitch.exe [OPTIONS]... INFILE OUTFILE\n");
printf("adds tray switching information to postscript file\n\n");
printf("Option list: \n");
printf("-o sets output to stdout\n");
printf("--help print help to screen\n");
printf("\n");
printf("inputfile example: traySwitch.exe test.ps\n");
printf("If infile is left out, then stdin is used for input.\n");
printf("If outfile is a filename, then that file is used.\n");
printf("If there is no outfile, then infile-EDIT.ps is used.\n");
printf("There cannot be an outfile without an infile.\n");
return 0;
}
//Get the filename off the command line and redirect it to input
//if there is no filename or it is a - then use stdin.
if (optind < argc) {
FILE *file;
file = fopen(argv[optind], "rb");
if (!file) {
fprintf(stderr, "Flex could not open %s\n",argv[optind]);
exit(1);
}
yyin = file;
strcpy(inputName, argv[optind]);
}
else {
printf("no input file set, using stdin. Press ctrl-c to quit");
yyin = stdin;
strcpy(inputName, "\b\b\b\b\bagainst stdin");
}
//Increment current place in argument list
optind++;
/********************************************
If no input name, then output set to stdout.
If no output name then copy input name and add -EDIT.csv.
If input name is '-' then output set to stdout,
otherwise use output name.
*********************************************/
if (optind > argc) {
yyout = stdout;
}
else if (output_flag == 1) {
yyout = stdout;
}
else if (optind < argc){
outfile = fopen(argv[optind], "wb");
if (!outfile) {
fprintf(stderr, "Flex could not open %s\n",FileName);
exit(1);
}
yyout = outfile;
}
else {
strncpy(FileName, argv[optind-1], strlen(argv[optind-1])-4);
FileName[strlen(argv[optind-1])-4] = '\0';
strcat(FileName, "-EDIT.ps");
outfile = fopen(FileName, "wb");
if (!outfile) {
fprintf(stderr, "Flex could not open %s\n",FileName);
exit(1);
}
yyout = outfile;
}
yylex();
if (output_flag == 0) {
fclose(yyout);
}
printf("Flex program finished running file %s\n", inputName);
return 0;
}