{"id":2728,"date":"2021-09-30T14:13:31","date_gmt":"2021-09-30T14:13:31","guid":{"rendered":"http:\/\/www.bardecode.com\/en1\/?p=2728"},"modified":"2021-09-30T14:21:38","modified_gmt":"2021-09-30T14:21:38","slug":"handling-pdf-files-docker-2","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/en1\/handling-pdf-files-docker-2\/","title":{"rendered":"Handling PDF files with Docker"},"content":{"rendered":"<p>Here&#8217;s an example of how PDF files could be handled on Docker using Ghostscript, though the same idea could be adopted for any third party conversion tool.<\/p>\n<p>Note that this is using <a href=\"https:\/\/www.nuget.org\/packages\/softekbarcodenetstandard\/\">SoftekBarcodeNetStandard from NuGet<\/a>.<\/p>\n<p><strong>1. Add the following to Dockerfile to install Ghostscript<\/strong><\/p>\n<p>RUN apt-get update &amp;&amp; apt-get install -y ghostscript<\/p>\n<p><strong>2. Use code similar to the following<\/strong><\/p>\n<p>SoftekBarcodeNetStandard.BarcodeReader barcode = new SoftekBarcodeNetStandard.BarcodeReader();<\/p>\n<p>\/\/ This assumes that the input file is in a stream but it should be easy to adapt for a file already on disk<br \/>\n\/\/ IFormFile imageFile<br \/>\nStream s = imageFile.OpenReadStream();<\/p>\n<p>int n;<\/p>\n<p>if (imageFile.ContentType == &#8220;application\/pdf&#8221;)<br \/>\n{<br \/>\nString pdfFile = System.IO.Path.GetTempFileName();<\/p>\n<p>\/\/ Save the stream to disk &#8211; miss if file already available<br \/>\nvar fileStream = System.IO.File.Create(pdfFile);<br \/>\ns.CopyTo(fileStream);<br \/>\nfileStream.Close();<\/p>\n<p>\/\/ Temp file for the TIF<br \/>\nString tifFile = System.IO.Path.GetTempFileName();<\/p>\n<p>\/\/ Call gs to convert from PDF to TIF<br \/>\nResult = ShellHelper.Bash(&#8220;gs -dNOPAUSE -r300 -sDEVICE=tiffscaled24 -sCompression=lzw -dBATCH -sOutputFile=&#8221; + tifFile + &#8221; &#8221; + pdfFile);<\/p>\n<p>\/\/ Scan the barcode from the TIF<br \/>\nn = barcode.ScanBarCode(tifFile);<\/p>\n<p>\/\/ Delete the temp files &#8211; maybe not for the PDF if not using stream input<br \/>\nSystem.IO.File.Delete(pdfFile);<br \/>\nSystem.IO.File.Delete(tifFile);<\/p>\n<p>}<br \/>\nelse<br \/>\n{<br \/>\nn = barcode.ScanBarCodeFromStream(s);<br \/>\n}<\/p>\n<p>ShellHelper code (from <a href=\"https:\/\/loune.net\/2017\/06\/running-shell-bash-commands-in-net-core\/\">https:\/\/loune.net\/2017\/06\/running-shell-bash-commands-in-net-core\/<\/a>):<\/p>\n<p>using System;<br \/>\nusing System.Diagnostics;<\/p>\n<p>public static class ShellHelper<br \/>\n{<br \/>\npublic static string Bash(this string cmd)<br \/>\n{<br \/>\nvar escapedArgs = cmd.Replace(&#8220;\\&#8221;&#8221;, &#8220;\\\\\\&#8221;&#8221;);<\/p>\n<p>var process = new Process()<br \/>\n{<br \/>\nStartInfo = new ProcessStartInfo<br \/>\n{<br \/>\nFileName = &#8220;\/bin\/bash&#8221;,<br \/>\nArguments = $&#8221;-c \\&#8221;{escapedArgs}\\&#8221;&#8221;,<br \/>\nRedirectStandardOutput = true,<br \/>\nUseShellExecute = false,<br \/>\nCreateNoWindow = true,<br \/>\n}<br \/>\n};<\/p>\n<p>process.Start();<br \/>\nstring result = process.StandardOutput.ReadToEnd();<br \/>\nprocess.WaitForExit();<\/p>\n<p>return result;<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example of how PDF files could be handled on Docker using Ghostscript, though the same idea could be adopted for any third party conversion tool. Note that this is using SoftekBarcodeNetStandard from NuGet. 1. Add the following to Dockerfile to install Ghostscript RUN apt-get update &amp;&amp; apt-get install -y ghostscript 2. Use code<\/p>\n<p><a href=\"https:\/\/www.bardecode.com\/en1\/handling-pdf-files-docker-2\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":337,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,31],"tags":[],"class_list":["post-2728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge-base","category-software-development-kits"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/2728"}],"collection":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/comments?post=2728"}],"version-history":[{"count":2,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/2728\/revisions"}],"predecessor-version":[{"id":2730,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/2728\/revisions\/2730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media\/337"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media?parent=2728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/categories?post=2728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/tags?post=2728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}