Guest User

Untitled

a guest
Nov 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. ps2pdf myfile.ps
  2.  
  3. PLOT SIZE:8.02x8.62Inches
  4. Magnification:7354.21X
  5.  
  6. gs -o myfile.pdf
  7. -sDEVICE=pdfwrite
  8. -g5775x6207
  9. -dPDFFitPage
  10. myfile.ps
  11.  
  12. #! /usr/bin/env perl
  13. use strict;
  14. use warnings;
  15.  
  16. use Scalar::Util qw(looks_like_number);
  17. use List::Util qw(all);
  18.  
  19.  
  20. sub ps2pdf;
  21. sub get_ps_headers;
  22. sub get_media_size;
  23. sub main;
  24.  
  25. # Run the program
  26. main();
  27.  
  28.  
  29. # Function: main
  30. #
  31. # Program's entry point.
  32. #
  33. sub main {
  34. for (@ARGV) {
  35.  
  36. # check input file
  37. if(not -r) {
  38. print "WARN: Cannot read input file: $_n";
  39. next;
  40. }
  41.  
  42. # build PDF file name
  43. my $pdf = $_;
  44. $pdf =~ s/(.e?ps)?$/.pdf/i;
  45.  
  46. ps2pdf($_, $pdf);
  47. }
  48. }
  49.  
  50.  
  51. # Function: ps2pdf
  52. #
  53. # Converts a PostScript file to PDF format using GhostScript,
  54. # keeping the medium size.
  55. #
  56. # Params:
  57. #
  58. # $ps_file - (string) Input [E]PS file name
  59. # $pdf_file - (string) Output PDF file name
  60. #
  61. sub ps2pdf {
  62. my ($ps_file, $pdf_file) = @_;
  63. my $cmd = "gs -q -sDEVICE=pdfwrite -dPDFFitPage ";
  64.  
  65. # try to find the media size
  66. my ($width, $height) = get_media_size(get_ps_header($ps_file));
  67.  
  68. # keep media size
  69. if(defined $height) {
  70. $cmd .= "-g${width}x${height} ";
  71. }
  72.  
  73. # set input/output
  74. $cmd .= "-o $pdf_file $ps_file";
  75.  
  76. print "Running: $cmdn";
  77.  
  78. system($cmd);
  79. }
  80.  
  81.  
  82. # Function: get_media_size
  83. #
  84. # Computes the size of a PostScript document in pixels,
  85. # from the headers in the PS file.
  86. #
  87. # Params:
  88. #
  89. # $hdr - (hash ref) Parsed PS header values
  90. #
  91. # Returns:
  92. #
  93. # On success: Two-element array holding the document's width and height
  94. # On failure: undef
  95. #
  96. sub get_media_size {
  97. my ($hdr) = @_;
  98.  
  99. # we need the DocumentMedia header
  100. return undef if not defined $hdr->{DocumentMedia};
  101.  
  102. # look for valid values
  103. my @values = split(/s+/, $hdr->{DocumentMedia});
  104. return undef if scalar @values < 3;
  105. my ($width, $height) = @values[1, 2];
  106.  
  107. return undef if not all { looks_like_number($_) } ($width, $height);
  108.  
  109. # Ghostscript uses a default resolution of 720 pixels/inch,
  110. # there are 72 PostScript points/inch.
  111. return ($width*10, $height*10);
  112. }
  113.  
  114.  
  115. # Function: get_ps_header
  116. #
  117. # Parses a PostScript file looking for headers.
  118. #
  119. # Params:
  120. #
  121. # $ps_file - (string) Path of the input file
  122. #
  123. # Returns:
  124. #
  125. # (hash ref) - As expected, keys are header names,
  126. # values are corresponding header values. A special key
  127. # named `version' is included for headers of the type
  128. # `PS-Adobe-3.0'
  129. #
  130. sub get_ps_header {
  131. my ($ps_file) = @_;
  132. my %head;
  133.  
  134. open my $fh, "<$ps_file" or die "Failed to open $ps_filen";
  135. while(<$fh>) {
  136. # look for end of header
  137. last if /^%%EndCommentsb/;
  138.  
  139. # look for PS version
  140. if(/^%!(w+)/) {
  141. $head{version} = $1;
  142. }
  143.  
  144. # look for any other field
  145. # Ex: %%BoundingBox: 0 0 1008 612
  146. elsif(/^%%(w+)s*:s*(.*S)/) {
  147. $head{$1} = $2;
  148. }
  149.  
  150. # discard regular comments and blank lines
  151. elsif(/^s*(%.*)?$/) {
  152. next;
  153. }
  154.  
  155. # any other thing will finish the header
  156. else {
  157. last;
  158. }
  159. }
  160.  
  161. return %head;
  162. }
Add Comment
Please, Sign In to add comment