如何修改这个 1992 年的纯文本到 PDF 转换 PostScript 以实际指定页面大小? [英] How to modify this plaintext-to-PDF-converting PostScript from 1992 to actually specify a page size?

查看:90
本文介绍了如何修改这个 1992 年的纯文本到 PDF 转换 PostScript 以实际指定页面大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将纯文本文档转换为 PDF.唯一接近实际工作的方法是安装GhostScript",然后使用以下 PostScript 脚本,由 SE 用户 @RedGrittyBrick(感谢)挖掘,该脚本采用纯文本文档(在脚本下方)并生成 PDF来自它.

它在技术上是有效的,但在视觉上混淆了每个页面的顶部和左边距,使得顶部边距变得太多"而左边距有点太适合"(与右侧相比)利润).至少在 SumatraPDF 中查看时,这是我唯一的 PDF 查看器.

脚本声明:

/topmargin 1 英寸定义/leftmargin 1 英寸定义

但是,视觉上看起来上边距可能是 4 英寸而不是 1 英寸文件里说.如果我将其修改为 0,则完成的 PDF 视觉 似乎具有 1 英寸的上边距.另一方面,如果我将 leftmargin 修改为 0 英寸,它会一直移动到左边框.

在视觉上看起来正确的方式,在顶部/右侧/底部/左侧具有适当的、均匀的边距,是:

/topmargin 0 英寸定义/leftmargin 0.8 英寸定义

但我不能就这样保留它,因为它很可能会在其他人的计算机/PDF 查看器上损坏.即使没有,我仍然真的很烦我,我不明白发生了什么.

有人告诉我,发生这种情况的原因是 PostScript 没有指定页面大小".但是,我不知道如何将其指定到文档中,也不知道脚本的作者一开始就没有这样做过.这似乎是一个基本的重大错误,但提供给我的人声称已经在许多不同的环境中成功使用了多年,这意味着什么?SumatraPDF 有非常奇特的默认设置吗?那个人的标准很低?说我要疯了?我真的不知道该怎么做,或者如何解决它.

我认为 PDF 的全部意义在于始终创建 1:1 副本,在尺寸和呈现方式方面没有任何歧义......显然不是.这是脚本:

%!%% 来自:乔纳森·蒙萨拉特 (jgm@cs.brown.edu)% 主题:PostScript ->ASCII *和* ASCII ->PostScript 程序% 新闻组:comp.lang.postscript% 日期:1992-10-01 04:45:38 PST%% "如果有人感兴趣,这里有一个有趣的程序% 布朗大学的约翰休斯教授格式化 ASCII% 在 PostScript 中,没有任何类型的机器生成器."%%%%%%% 计划:%%% 以空字符串开头.%%% 对于输入流中的每个字符,%%% 检查是否是回车.%%% 如果是,则显示当前字符串并将其重置为空%%% 如果不是,则将其添加到当前字符串中./Courier findfont 10 scalefont setfont %% 选择固定宽度字体/行高currentfont/FontBBox 得到 %% bbox bbox0 2 getinterval %% bbox {xm ym}exch %% {xm ym} bbox2 2 getinterval %% {xm ym} {xM yM}加载弹出 %% {xm ym} xM yM3 2 卷 %% xM yM {xm ym}加载流行音乐currentfont/FontMatrix 得到 %% xM yM xm ym MAT变换 %% xM yM xm' ym'4 2 卷currentfont/FontMatrix 得到 %% xm' ym' xM yM MAT变换 %% xm' ym' xM' yM'exch pop %% xm' ym' yM'子 %% xm' ym'-yM'exch pop %% dy否定定义行高 pstack pop/str 500 string def %% 存储长字符串的空间.../empty 500 string def %% 要使用的空字符串/stringindex 0 def %% 我们填充字符串的程度/inch {72 mul } def %% 一个有用的工具.../pageheight 11 英寸定义/topmargin 1 英寸定义/botmargin 1 英寸定义/leftmargin 1 英寸定义/linesperpage pageheight topmargin sub botmargin sub lineheight div cvi def/linenumber 1 def %% 我们要写的行/newline { %% 移动到新行;如有必要,刷新页面linenumber linesperpage gt {/linenumber 1 def showpage } ifleftmargin pageheight topmargin sub linenumber lineheight mul sub moveto/linenumber linenumber 1 添加定义定义/cleanup { %% 打印出你在那里的最后一点...str 显示页面定义/startstring { %% 清空字符串并重置其计数器.str 0 空 putinterval/stringindex 0 def定义/showstring { %% 在新行上打印字符串并刷新它新队字符串显示起始字符串定义堆栈/addtostring { %% 将另一个字符放入字符串中,如果有空间dup 500 gt {pop}{str exch stringindex exch put/stringindex stringindex 1 添加 def} ifelse定义%% 主程序:获取字符并处理它们%{当前文件读取 {}{cleanup exit} ifelsedup 10 eq %% 如果是回车...{pop showstring} %% 写出这行文本并重新开始{dup 0 eq %% 如果它是文件结束标记...{退出} %% 停止!{addtostring} %% 否则,将字符添加到当前字符串如果别的}ifelse %% 示例数据如下.} 环形

然后我运行:

ps2pdf in.ps out.pdf

解决方案

你的 Y 问题是如何更新一个在 Windows 成熟之前的时代非常先进的 GS 程序,以在现代寡妇 X 系统上工作.

专家级 GS 编写者/维护者已尝试就此提出建议,但如今有非常简单的方法可以在 Windows 中回归该 XY 任务.

Windows 使用记事本处理纯文本,您只需设置一次字体和边距.然后自动手动它要么右键单击打印";或者在命令行上使用打印/PT 选项,然后 NotePad 将使用任何 PS 驱动程序(例如 GhostScript pdf writer 或更轻松地 MS Print to PS/PDF)对其进行格式化.您还应该知道,SumatraPDF 可以读取纯文本并具有可以打印到 Image.PDF 的命令行打印功能.

所以 PrintScript 有很多方法可以得到一个 Text 或 Image 结构的 PostScripted.PDF

我建议的编码顺序是
a) 通过 GUI 或 CLI 使用打印管理来准备您喜欢的自定义表单,或者最简单地使用系统默认的 A4 或 Letter 页面比例.

b) 复制内置的 PDF 驱动程序或添加任何其他虚拟 PS/PDF 驱动程序并将其重定向到 NonPromptPort(您可以使用默认的 PromptPort 进行比较交互测试)我将 Mine 设置为 C:\MyData\PrintOut.PDF

c) 将记事本配置为所需的页面形式,方向,即横向或纵向和边距,如果您在 SumatraPDF 中保持先前的输出 run.pdf 打开,您甚至可以观看它编译(如在 LaTeX 中)并出现在您的眼前在 SumatraPDF 中,因为它不锁定小 PDF

d) 编写一个 1 liner cmd(或复杂的错误检查),以便允许拖放或其他批处理自动化,将其称为 TXT2PDF.CMD 您可以在必要时添加第三个和第四个参数,但我喜欢保持简单,所以将 %2 设置为重定向的端口驱动程序.

%SystemRoot%\system32\notepad.exe/pt "%1";我的打印到 PDF"复制 C:\MyData\PrintOut.PDF "%~dpn1.pdf";SumatraPDF%~dpn1.pdf"

或基于自定义格式图像的 PDF 使用最新的 SumatraPDF Pre-Release

SumatraPDF -print-to My Print to PDF"-打印设置paperkind=A4L"%1"

输出将是 PAI 因此不可选择的文本,并注意在 v3.2 或之前必须设置

EbookUI [...UseFixedPageUI = True

以便查看/打印句柄 TXT(即 v3.3 不需要)

PostScriptum :-)

我忘了提及您是否喜欢您的 TXT,例如对齐然后像这样在写字板 rtf 中格式化它

访问

注意仔细观察,看看它是如何回流的!因为 PDF 输出的边距可能与保存时 RTF 的边距不完全相同.

I'm trying to convert a plaintext document to PDF. The only method which has come anywhere close to actually working is installing "GhostScript" and then using the following PostScript script, dug up by the SE user @RedGrittyBrick (thanks), which takes a plaintext document (underneath the script) and produces a PDF from it.

It technically works, but visually messes up the top and left margins for each page in such a manner that the top margin becomes "way too much" and the left margin is "a bit too for in" (compared to the right margin). At least when viewed in SumatraPDF, which is the only PDF viewer I have.

The script states:

/topmargin 1 inch def
/leftmargin 1 inch def

However, it visually looks like the top margin is maybe 4 inches and not 1 inch as it says in the file. If I modify it to 0, the finished PDF visually appears to have 1 inch top margin. If I, on the other hand, modify the leftmargin to 0 inch, it goes all the way to the left border.

The way it visually looks right to me, with proper, even margins on top/right/bottom/left, is:

/topmargin 0 inch def
/leftmargin 0.8 inch def

But I can't just keep it like that as it will more than likely break on others' computers/PDF viewers. And even if it doesn't, I still really bugs me that I don't understand what is going on.

I have been told that the reason for this happening is that the PostScript does not specify a "page size". However, I have no idea how I would specify this into the document, nor how it can be possible that the author of the script never did this in the first place. It seems like such a basic, major mistake, yet the person who gave it to me claims to have used it for many years in many different environments successfully, so what does that mean? That SumatraPDF has very exotic default settings? That the person in question has very low standards? That I'm going insane? I really don't know what to make of this, or how to fix it.

I thought the whole point of PDF was to always create a 1:1 copy, with no ambiguity whatsoever in the dimensions and how things will be rendered... Apparently not. This is the script:

%!
%
% From: Jonathan Monsarrat (jgm@cs.brown.edu)
% Subject: PostScript -> ASCII *and* ASCII -> PostScript programs
% Newsgroups: comp.lang.postscript
% Date: 1992-10-01 04:45:38 PST 
%
% "If anyone is interested, here is an interesting program written by
% Professor John Hughes here at Brown University that formats ASCII
% in PostScript without a machine generator of any kind."
%
%%%
%%% Plan:
%%% Start with an empty string.
%%% For each character in the input stream, 
%%%    check to see if it's a carriage return.
%%%    if so, show the current string and reset it to empty
%%%    if not, add it to the current string.

/Courier findfont 10 scalefont setfont  %% Choose a fixed width font
/lineheight 
currentfont /FontBBox get dup      %% bbox bbox
0 2 getinterval    %% bbox {xm ym}
exch     %% {xm ym} bbox
2 2 getinterval    %% {xm ym} {xM yM}
aload pop    %% {xm ym} xM yM
3 2 roll     %% xM yM {xm ym}
aload pop
currentfont /FontMatrix get  %% xM yM xm ym MAT
transform    %% xM yM xm' ym'
4 2 roll
currentfont /FontMatrix get  %% xm' ym' xM yM MAT
transform    %% xm' ym' xM' yM'
exch pop     %% xm' ym' yM'
sub     %% xm' ym'-yM'
exch pop    %% dy
neg def 

lineheight pstack pop

/str 500 string def   %% Room to store a long string...
/empty 500 string def   %% An empty string to work with
/stringindex 0 def   %% How far we've filled the string
/inch {72 mul } def   %% A useful tool...
/pageheight 11 inch def
/topmargin 1 inch def
/botmargin 1 inch def
/leftmargin 1 inch def
/linesperpage pageheight topmargin sub botmargin sub lineheight div cvi def
/linenumber 1 def   %% the line we're about to write on

/newline {   %% move to a new line; flush page if necessary
   linenumber linesperpage gt {/linenumber 1 def showpage } if
   leftmargin pageheight topmargin sub linenumber lineheight mul sub moveto
   /linenumber linenumber 1 add def
} def

/cleanup {  %% print out the last bit of whatever you had there...
   str show showpage
} def

/startstring {  %% empty the string and reset its counter.
   str 0 empty putinterval
   /stringindex 0 def
} def

/showstring {  %% print the string on a new line and flush it
   newline
   str show 
   startstring
} def

pstack 

/addtostring {  %% put another character in the string, if there's room
   dup 500 gt {pop}{str exch stringindex exch put
   /stringindex stringindex 1 add def} ifelse
} def

%
% Main program: get characters and deal with them
%
{
   currentfile read {}{cleanup exit} ifelse
   dup 10 eq                   %% if it's a carriage return...
      {pop showstring}         %% write out this line of text and start over
      {dup 0 eq         %% if it's an end-of-file mark...
       {exit}                %% stop!
       {addtostring}           %% otherwise, add the character to current string
       ifelse}
      ifelse                   %% Sample data follows.
} loop

I then run:

ps2pdf in.ps out.pdf

解决方案

Your Y question is how to update a GS program that was highly advanced in its own era before windows was of age, to work on a modern widows X system.

The expert GS writers/maintainers have tried to advise on that, however today there are ever so simple ways to regress that XY task in windows.

Windows uses NotePad to handle PlainText in such a way that all you need to do is there set the fonts and margins once. Then Automanually it is either right click "Print" OR on the command Line use prining /PT option and then NotePad will format it using any PS driver such as GhostScript pdf writer or more easily MS Print to PS/PDF. Also you should Know that SumatraPDF can read PlainText and has command line printing which can be to Image.PDF.

So there are many ways to PrintScript to get a Text or Image structured PostScripted.PDF

I suggest the coding sequence is
a) Use Print Management via GUI or CLI to prepare your preferred custom Form or most simply use the system default A4 or Letter page ratio.

b) Either Duplicate the built in PDF driver or add any other virtual PS/PDF driver and redirect it to a NonPromptPort (You can use the default PromptPort for comparative interactive testing) I set Mine to C:\MyData\PrintOut.PDF

c) Configure NotePad to the desired Page Form, Orientation i.e. Landscape or Portrait and margins, If you keep the previous output run.pdf open in SumatraPDF you can even watch it compile (like in LaTeX) and appear before your very eyes within SumatraPDF, since it does not lock small PDFs

d) Write a 1 liner cmd (or convoluted with error checks), so as to allow for drag and drop or other batched automation, call it TXT2PDF.CMD you can add the third and fourth arguments if necessary but I like to Keep It Stupidly Simple, so set %2 to your redirected port driver.

%SystemRoot%\system32\notepad.exe /pt "%1" "My Print to PDF"
Copy C:\MyData\PrintOut.PDF "%~dpn1.pdf"
SumatraPDF "%~dpn1.pdf"

OR for custom format image based PDF use latest SumatraPDF Pre-Release

SumatraPDF -print-to "My Print to PDF" -print-settings "paperkind=A4L" "%1"

Where the output will be PAI thus non-selectable text, and note that in v3.2 or before you must set

EbookUI [
...
    UseFixedPageUI = True

so as to view / print handle TXT (that is NOT needed for v3.3)

PostScriptum :-)

I forgot to mention if you like your TXT pretty e.g. Justified then format it in WordPad rtf like this

drop in on Doc2PDF.cmd

and it Auto opens in SumatraPDF like this

NOTE Look closely and see how it reflows ! since the PDF output may not be EXACTLY the same margins as the RTF when it was saved.

这篇关于如何修改这个 1992 年的纯文本到 PDF 转换 PostScript 以实际指定页面大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆