我有myfile.ps
包含矢量图像。
但是当我跑步时
ps2pdf myfile.ps
输出页面大小似乎是A4:矢量图像太大而被切掉,所以丢失了大约一英寸。
除原始矢量图像外,还在输出PDF文件中打印以下伪标题:
PLOT SIZE:8.02x8.62Inches
Magnification:7354.21X
是否有任何选项或任何方式将PS文件转换为PDF 保留原始纸张尺寸?
答案 0 :(得分:12)
如果输入的postscript具有EPS BoundingBox
,则应该保留页面大小:
ps2pdf -dEPSCrop <input.ps> <output.pdf>
答案 1 :(得分:7)
我怀疑你引用的2行是否真的在PS文件中被引用... Aren是否在 %
注释字符之前?
如果 没有 在这些字符之前,则不会使用PS解释器,因为它们不是已知的PostScript运算符。
如果 在此类字符之前,则PS解释器会忽略它们,因为......它们只是注释! : - )
如果要将此PS文件转换为PDF,最好直接运行Ghostscript(ps2pdf
只是围绕Ghostscript命令的瘦shell脚本包装器):
gs -o myfile.pdf \
-sDEVICE=pdfwrite \
-g5775x6207 \
-dPDFFitPage \
myfile.ps
<强>解释强>
-g...
以像素提供中等大小。595x842pt
(PostScript点数)。595x842pt == 5950x8420px
。8.02x8.62Inches ≈≈ 5775x6207px
。答案 2 :(得分:2)
我不被允许发表评论,但我必须警告所有人,目前所有答案都容易受到恶意附言文件的攻击。</ p>
像这样使用gs
非常危险。 ps2pdf
在内部使用-dSAFER
选项,例如,这会阻止不受信任的postscript文件加密您的文件并呈现需要您为解密密钥支付赎金的pdf!总是使用-dSAFER
!
虽然-o outputFile.pdf
很好,但从版本9.23开始,它也没有记录(通过手册页或gs -h
)。
以下命令的工作方式与其他解决方案一样,无需担心顶部被切断:
gs -sOutputFile=file.pdf -dNOPAUSE -dBATCH -sPAPERSIZE=a4 -sDEVICE=pdfwrite -dSAFER file.ps
-sPAPERSIZE=a4
是指定a4纸张尺寸的方式。
要获取页面大小,您可以查找如下所示的行:
%%PageBoundingBox:·12·12·583·830
然后使用
gs -sOutputFile=file.pdf -dNOPAUSE -dBATCH -g583x830 -r72 -sDEVICE=pdfwrite -dSAFER file.ps
它完美无缺。
答案 3 :(得分:0)
基于@Kurt Pfeifle的回答我写了这个Perl脚本来完成任务:
#! /usr/bin/env perl
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
use List::Util qw(all);
sub ps2pdf;
sub get_ps_headers;
sub get_media_size;
sub main;
# Run the program
main();
# Function: main
#
# Program's entry point.
#
sub main {
for (@ARGV) {
# check input file
if(not -r) {
print "WARN: Cannot read input file: $_\n";
next;
}
# build PDF file name
my $pdf = $_;
$pdf =~ s/(\.e?ps)?$/.pdf/i;
ps2pdf($_, $pdf);
}
}
# Function: ps2pdf
#
# Converts a PostScript file to PDF format using GhostScript,
# keeping the medium size.
#
# Params:
#
# $ps_file - (string) Input [E]PS file name
# $pdf_file - (string) Output PDF file name
#
sub ps2pdf {
my ($ps_file, $pdf_file) = @_;
my $cmd = "gs -q -sDEVICE=pdfwrite -dPDFFitPage ";
# try to find the media size
my ($width, $height) = get_media_size(get_ps_header($ps_file));
# keep media size
if(defined $height) {
$cmd .= "-g${width}x${height} ";
}
# set input/output
$cmd .= "-o $pdf_file $ps_file";
print "Running: $cmd\n";
system($cmd);
}
# Function: get_media_size
#
# Computes the size of a PostScript document in pixels,
# from the headers in the PS file.
#
# Params:
#
# $hdr - (hash ref) Parsed PS header values
#
# Returns:
#
# On success: Two-element array holding the document's width and height
# On failure: undef
#
sub get_media_size {
my ($hdr) = @_;
# we need the DocumentMedia header
return undef if not defined $hdr->{DocumentMedia};
# look for valid values
my @values = split(/\s+/, $hdr->{DocumentMedia});
return undef if scalar @values < 3;
my ($width, $height) = @values[1, 2];
return undef if not all { looks_like_number($_) } ($width, $height);
# Ghostscript uses a default resolution of 720 pixels/inch,
# there are 72 PostScript points/inch.
return ($width*10, $height*10);
}
# Function: get_ps_header
#
# Parses a PostScript file looking for headers.
#
# Params:
#
# $ps_file - (string) Path of the input file
#
# Returns:
#
# (hash ref) - As expected, keys are header names,
# values are corresponding header values. A special key
# named `version' is included for headers of the type
# `PS-Adobe-3.0'
#
sub get_ps_header {
my ($ps_file) = @_;
my %head;
open my $fh, "<$ps_file" or die "Failed to open $ps_file\n";
while(<$fh>) {
# look for end of header
last if /^%%EndComments\b/;
# look for PS version
if(/^%!(\w+)/) {
$head{version} = $1;
}
# look for any other field
# Ex: %%BoundingBox: 0 0 1008 612
elsif(/^%%(\w+)\s*:\s*(.*\S)/) {
$head{$1} = $2;
}
# discard regular comments and blank lines
elsif(/^\s*(%.*)?$/) {
next;
}
# any other thing will finish the header
else {
last;
}
}
return \%head;
}