2015-05-08 349 views
7

我有myfile.ps與包括一個矢量圖像。 但是當我運行ps2pdf:保留頁面大小

ps2pdf myfile.ps 

似乎輸出頁面大小爲A4:矢量圖像過大,成爲切掉,所以大約一英寸丟失。

下面的僞報頭在輸出的PDF文件的印刷,除原來的矢量圖像:

PLOT SIZE:8.02x8.62Inches 
Magnification:7354.21X 

是否有任何選項或任何方式對PS文件轉換爲保留一個PDF 原始紙張尺寸?

回答

6

我懷疑你引用的2行是否真的在PS文件裏面引用過......不是他們前面有%評論字符?

  • 如果他們不是通過這樣的人物preceeded,沒有PS解釋會的工作,因爲他們沒有已知的PostScript運營商。

  • 如果他們通過這樣的字符preceeded,PS解釋會簡單地忽略他們,因爲......他們只是評論! :-)

如果你想這個PS文件轉換爲PDF,最好是直接運行的Ghostscript(ps2pdf只有大約一個Ghostscript的命令薄shell腳本包裝反正):

gs -o myfile.pdf  \ 
    -sDEVICE=pdfwrite \ 
    -g5775x6207  \ 
    -dPDFFitPage  \ 
    myfile.ps 

說明:

  1. -g...給出像素的中等大小。
  2. A4頁面的尺寸爲595x842pt(PostScript點數)。
  3. 1英寸與72 PostScript點相同。
  4. 默認情況下,默認情況下,Ghostscript在處理PDF輸出時計算每英寸720像素的分辨率。
  5. 因此對於PDF輸出595x842pt == 5950x8420px
  6. 因此對於您的案件8.02x8.62Inches ≈≈ 5775x6207px
+0

你說得對:我把輸出的PDF頭與PS頭混在一起,錯誤地假設它們是同一件事。現在我糾正了這個問題。不幸的是,隨着你的價值觀,圖像仍然被刪除,也許是因爲「標題」不是很值得信賴。但是將輸出尺寸增加幾百個像素就可以得到正確的結果。謝謝! – BowPark

9

如果輸入後記有EPS BoundingBox,這應該保留頁面大小:

ps2pdf -dEPSCrop <input.ps> <output.pdf>

+0

試過這個,但沒有奏效。 – roy

+0

@roy什麼版本的'ps2pdf' /'ghostscript'?錯誤信息是什麼?你的輸入postscript是否包含一個邊界框?您似乎已經降低了投票率,但是您沒有提供很多信息。 – Raman

+0

未顯示錯誤,但生成的PDF不保留頁面大小。正如你所說,也許這個選項讀取邊界框,但這個問題是關於頁面大小,它存儲在DocumentMedia標題中,請參閱下面的答案。 – roy

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; 
}