#include #include #include using namespace std; /** * \brief Command used for transforming from eps to pdf */ enum EpsToPdfProgram { Ghostscript, ///< Converts and crops Eps2Pdf ///< Converts and autorotates but does not crop }; /** * \brief Convert EPS to PDF * \param fileInput EPS input file name * \param fileOutput PDF output file name * \param clean remove temporary intermediate files (the input EPS and the bounding box) * \param margin extra pixels when adjusting the size of the figure of the eps file * \param program used program * \return string with command used for converting to pdf */ std::string TransformEpsToPdf(const std::string fileInput, const std::string fileOutput, const bool clean=true, const int margin=5, EpsToPdfProgram program=Ghostscript); string TransformEpsToPdf(const string fileInput, const string fileOutput, const bool clean, const int margin, EpsToPdfProgram program) { string command=""; string boxTemporaryFile=""; if(program==Ghostscript) { //Calculating the Frame (writing it to temporary file .bbox) boxTemporaryFile=fileInput+".bbox"; command="gs -sDEVICE=bbox -dBATCH -dNOPAUSE "+fileInput+" 2>&1 | grep '%%HiResBoundingBox' | awk '{print $2, $3, $4, $5}'>"+boxTemporaryFile; if(system(command.c_str())){;} //Opening the .bbox temporary file and loading it to 4-array /** #bbox[0]=offset vertical superior (empezando desde arriba, apunta hacia abajo) #bbox[1]=offset lateral izquierdo (empezando desde la izqda, apunta hacia la dcha) #bbox[2]=offset vertical inferior (empezando desde arriba, apunta hacia abajo) #bbox[3]=offset lateral derecho (empezando desde la izqda, apunta hacia la dcha) #system("inkscape --export-area-drawing --export-pdf="+ file_map + ".pdf \\" + file_map + "_.pdf") */ Real_t bbox[4]; ifstream box(boxTemporaryFile.c_str()); for(int i=0;i<4;++i) { box >> bbox[i]; if(i<2) { bbox[i]-=margin; } else { bbox[i]+=margin; } } box.close(); //Adapting the size and converting to pdf command="gs -q -o "+fileOutput+" -sDEVICE=pdfwrite -c "+ "\"[/CropBox ["; string espacio=""; for(int i=0;i<4;++i) { command+=espacio+d2s(bbox[i]+margin); espacio=" "; } command+="] /PAGES pdfmark\" -f "+ fileInput; } else if(program==Eps2Pdf) { command="epstopdf --autorotate=All -outfile="+fileOutput+" "+fileInput; } else { cout << "Warning in CommonTools::TransformEpsToPdf: not valid program type.\n"; return ""; } if(system(command.c_str())){;} if(clean) { string commandclean; commandclean="rm "+fileInput; if(system(commandclean.c_str())){;} if(program==Ghostscript) { commandclean="rm "+boxTemporaryFile; if(system(commandclean.c_str())){;} } } if(DEBUG) { cout << "Note in CommonTools:TransformEpsToPdf: " << fileOutput << " generated.\n"; } //Return converting string command for appending it to the fileTemplate return command; }