-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 - --- title: "Obtaining Cropped Line Drawings From Simulink" date: 2018-10-12T13:38:23Z draft: false category: guide excerpt: "Guide for turning sprawling Simulink diagrams into crisp vector figures by printing to PDF, splitting with pdftk, and batch-cropping with pdfcrop. I finish with a shell script that automates the entire workflow." - --- # Diagrams, Diagrams Everywhere ## Motivation For my third year project I needed a way to put my ~wave:annoyingly large~ Simulink system into a document. Screenshots won't do because the quality is far too low :myeyes:, so I generally avoid putting raster images in reports. There clearly had to be a better way, which I hope show in this short but useful post. These methods will work with MacOS and \*nix systems. At the end of the post is a tool which will give you the ability to split and crop a multi page pdf all in one go, with some basic usage directions. ## Get the PDF This part is simple enough, press 'print' and a dialog box will pop up. Select 'print to file' and 'all systems', as highlighted in the image below: ![Printing](print.png) This should print a PDF file to some location specified on your system. This is fantastic! We have a line drawing representation of our diagram, albeit in a rather unusable form, we clearly need to split this file up and deal with it more from there. ## Split the PDF Fortunately we do not have to get too involved with PDF reading, as a tool exists to help us with PDF mainpulation! We can use _pdftk_ to split our multi-page PDF into single page PDFs by executing the following command: ```bash pdftk multipage.pdf burst ``` You may have to install _pdftk_, as it is not guaranteed as standard on \*nix. I really like _pdftk_, it gives you more tools for PDF manipulation than you would ever need, and it's free software! Here is a little aside where I (actually pulled from help files) list some of the answers to the question: _What can you do with pdftk?_ 1. Merge PDF Documents - - Split PDF Pages into a New Document - - Rotate PDF Pages or Documents - - Decrypt Input as Necessary (Password Required) - - Encrypt Output as Desired - - Fill PDF Forms with FDF Data or XFDF Data and/or Flatten Forms - - Apply a Background Watermark or a Foreground Stamp - - Report on PDF Metrics such as Metadata, Bookmarks, and Page Labels - - Update PDF Metadata - - Attach Files to PDF Pages or the PDF Document - - Unpack PDF Attachments - - Burst a PDF Document into Single Pages - - Uncompress and Re-Compress Page Streams - - Repair Corrupted PDF (Where Possible) ## Crop the PDFs Again the great UNIX gods pity us, another tool exists which allows us to crop PDF documents! The tool in question (a perl script!) is the appropriately-named _pdfcrop_, which comes pre-installed on MacOS and \*nix systems. To use _pdfcrop_, perform the following command: ```bash pdfcrop singlepage.pdf ``` _pdfcrop_ will then output a file called `singlepage-crop.pdf` in the same directory. Which when put together should give you a nicely cropped simulink line drawing! ## Automating ourselves out of the job Splitting and cropping that PDF was honestly a very involved process. _pdfcrop_ has to be called for each page, and the number of files involved becomes a small nightmare, especially if you make changes to your plant and need to update your diagrams. Fortunately I have taken the liberty in writing a small bash script, which can be used as following (after a `chmod +x splitcrop.sh`). ```bash ./splitcrop.sh multipage.pdf ``` For my Third Year project I ended up using this script, but with some commands to automatically rename the files taked onto the end, here's one for example: ```bash mv ./split_files/pg_0001-crop.pdf ../report/3yp_general/images/plant/Global.pdf ``` This may be useful if working with a group project and making lots of small changes, otherwise omit this. ## Splitcrop.sh ```bash #!bin/env/bash echo "Splitting PDF $1" # split the pdf and move all pages to a `split files' folder pdftk $1 burst mkdir split_files mv pg_0\* ./split_files/ filetype=".pdf" files=`find ./split_files/` # crop each pdf for file in $files do if [[ $file = *${filetype} ]]; then echo "Splitting $file" pdfcrop $file fi done # remove non-cropped files mv ./split*files/*-crop.pdf ./ rm ./split*files/* mv \*-crop.pdf ./split_files/ ``` -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEJtHIQX7gIiqRWdSs/RuAbpOsR8kFAmrxXn4ACgkQ/RuAbpOs R8mBAQ//UmWBLYj3btlMzlWwf8Rn4gjbxk9Zj3Grh4TaYC53vQwIHK8pG2Mj9UXn YKqveioGT10EHk8oYnfsgTKeSBjzhry5FMNyq49B5cxl3/WuaIAfD90IcB46EH7V Err6eLqhgjsFfvSjNDcNxAcYdyJKO90lnMVEdQLtdqTFG1/NdaYUEO3I23yqs8RP AKHgBaUS2zI6ShTErCJdKECyY5K+OoEb6seLlzlyowHfG9mWAe+ayj7dEIK8FLB3 gNyO8KyvO8/rsKous5NTLRoDRVDMeFh81zKg3fmN7PbAFq/2tgnPkSyjfSTsE2yu s3ex6Vc/Az5QjGRPcpeMSN9gJR75zDT+rXPe2YaU9dI5ZQH8FgmGWHHrHV4Lz5Ab 5Xb1p6pWf/5833ZcJ8lW1Nz8rJx/pZSq1AVqtLGx++XERbx2CkYpddCXgTFg4bK/ eGGFa3SalgTl7Bl/yfiq48kVwHdCcCA54A5JzFsdR/7QFaRyOF6CT+4s26J9khdK SZUg+pf3es+nF+Vxg4hzJKbKzVFnK2MAMneUxQZoGYd6K2Y+sY8kwY2XhxJ4oJ6S GR52OcxMljiR2Jyx6SIyLNdbiiAe4ybv5hbx/yHUeb2u+56/8ssJTGcCZUE/zcTs 3qxe9XNgOl3FrgDylpWebECrOWOcXCvQ0O4y7kxREkwuTM6nQi0= =V97A -----END PGP SIGNATURE-----