4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://floralicense.org/license/
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include "pgen_debug.h"
24 #include "previewgen.h"
25 #include "preview_util.h"
30 #define CUPS_OPTION_PORTRAIT "orientation-requested=3"
31 #define CUPS_OPTION_LANDSCAPE "orientation-requested=4"
33 #define CUPS_FILTER_DIR "/usr/lib/cups/filter"
35 #define PPI_MAX_VAL 10000
36 #define PPI_STR_MAX_LEN sizeof("10000")
38 #define PDFGEN_CMDSTR_MAXLEN 1024
40 #define PDFUNITE_PATH "/usr/bin/pdfunite"
43 * @brief This function securely executes cmd line request with popen
44 * @param[in] cmd_str command line to execute
48 int call_cmd(const char *cmd_str)
52 //NOTICE : Use popen or fork/execv function instead of system() which has security hole.
54 FILE *cmd_output = popen(cmd_str, "r");
55 if (cmd_output == NULL) {
56 PGEN_DEBUG("popen error (%s)", cmd_str);
66 read_len = getline(&str_buf, &len, cmd_output);
67 } while (read_len != -1);
69 PGEN_IF_FREE_MEM(str_buf);
77 * @brief This function forms and executes cmd line request to CUPS
78 * pdftopdf filter to convert PDF file
79 * @param[in] path path to destination PDF file
80 * @param[in] outfile destination file name
81 * @param[in] settings several settings of generating PDF
85 int call_pdftopdf(const char *infile, const char *outfile,
86 const struct pdfgen_settings *settings)
90 PGEN_RETV_IF(infile == NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
91 -1, "Invalid argument");
96 /* placing n_up number of pages on the specified media */
97 snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR "/pdftopdf job user title 1 \"%s number-up=%d PageSize=%s fit-to-page=True pdftopdfAutoRotate=false\" \"%s\""
98 " >%s 2> " PREVIEW_TEMP_DIR "/errlog",
99 settings->ppd_filename,
100 PAGE_ORIENTATION_PORTRAIT == settings->orientation ? CUPS_OPTION_PORTRAIT :
101 PAGE_ORIENTATION_LANDSCAPE == settings->orientation ? CUPS_OPTION_LANDSCAPE : "",
103 settings->paper_name, infile, outfile);
105 PGEN_DEBUG("call_pdftopdf(): %s", cmd_str);
106 err_code = call_cmd(cmd_str);
107 PGEN_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
114 int strcat_img_scale_option(char *res, const char *img_path,
115 const struct page_scale *scale)
118 PGEN_RETV_IF(res == NULL , -1 , "res is NULL");
119 PGEN_RETV_IF(img_path == NULL , -1 , "img_path is NULL");
120 PGEN_RETV_IF(scale == NULL , -1 , "scale is NULL");
121 //PGEN_RETV_IF(scale->w == 0 , -1 , "scale->w is 0");
126 char ppi_str[PPI_STR_MAX_LEN + 1];
129 switch (scale->type) {
130 case SCALE_FIT_TO_PAPER:
131 strcat(res, "fit-to-page=true");
134 strcat(res, "scaling=100");
140 result = get_image_resolution(img_path, &imgw, &imgh);
141 PGEN_RETV_IF(result < 0, -1 , "Can't get resolution");
143 ppi = (double)imgw / scale->w;
144 PGEN_RETV_IF(ppi >= PPI_MAX_VAL, -1 , "Resolution is too big");
146 snprintf(ppi_str, PPI_STR_MAX_LEN, "%d", ppi);
147 ppi_str[PPI_STR_MAX_LEN] = '\0';
149 strcat(res, ppi_str);
156 * @brief This function forms and executes cmd line request to CUPS
157 * imagetopdf filter to convert image to PDF file
158 * @param[in] path path to destination PDF file
159 * @param[in] outfile destination file name
160 * @param[in] settings several settings of generating PDF
164 int call_imagetopdf(const char *infile, const char *outfile,
165 const struct pdfgen_settings *settings)
168 PGEN_RETV_IF(infile== NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
169 -1 , "Invalid argument");
176 strcat_img_scale_option(scale_str, infile, &(settings->scale));
178 /* placing n_up number of pages on the specified media */
179 snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR
180 "/imagetopdf job user title 1 \"%s"
181 " number-up=%d PageSize=%s"
185 " >%s 2> /tmp/mobileprint/errlog",
186 settings->ppd_filename,
187 PAGE_ORIENTATION_PORTRAIT == settings->orientation
188 ? CUPS_OPTION_PORTRAIT " "
189 : PAGE_ORIENTATION_LANDSCAPE == settings->orientation
190 ? CUPS_OPTION_LANDSCAPE " "
193 settings->paper_name,
194 /*SCALE_FIT_TO_PAPER == settings->scale.type
195 ? "True" : "False",*/
199 PGEN_DEBUG("call_imagetopdf(): %s", cmd_str);
200 err_code = call_cmd(cmd_str);
201 PGEN_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
208 int call_pdfunite(char **const infiles_z, const char *outfile)
211 char cmd_str[PDFGEN_CMDSTR_MAXLEN + 1];
215 PGEN_RETV_IF(NULL == infiles_z || NULL == infiles_z[0]
216 || NULL == outfile, -1, "Argument error");
219 strncat(cmd_str, PDFUNITE_PATH, PDFGEN_CMDSTR_MAXLEN);
222 while (infiles_z[i] != NULL) {
223 strncat(cmd_str, " ", PDFGEN_CMDSTR_MAXLEN);
224 strncat(cmd_str, infiles_z[i], PDFGEN_CMDSTR_MAXLEN);
228 strncat(cmd_str, " ", PDFGEN_CMDSTR_MAXLEN);
229 strncat(cmd_str, outfile, PDFGEN_CMDSTR_MAXLEN);
230 cmd_str[PDFGEN_CMDSTR_MAXLEN] = '\0';
232 err_code = call_cmd(cmd_str);