我正在尝试使用GTK软件中的HTML文件生成PDF。因此,运行中的软件会产生一个使用g_spawn_command_line_sync()创建PDF的过程,并等待PDF生成完成。
我正在使用wkhtmltopdf将HTML文件转换为PDF。下面是我的方法。
// Creating the HTML file
FILE * pFile;
pFile = fopen("/home/user_name/savelog.html","w");
system("sudo chown -R user_name /home/user_name/savelog.html");
char write_to_file[800];
// Writing to HTML file which will then be converted to the PDF file
sprintf(write_to_file,"<HTML> <p> <b> <font size= \"13\"> %s </font> </b> <br/> <br/> <b> OUTPUT 1 </b> <br/> No of Loops: %d <br/> Average Loop Time: %f secs <br/> Read Max : %f <br/> Read Min : %f <br/> Min Time : %f secs <br/> Max Time: %f secs <br/> <br/> <b> OUTPUT 2 </b> <br/> No of Loops: %d <br/> Average Loop Time: %f secs <br/> Read Max : %f <br/> Read Min : %f <br/> Min Time : %f secs <br/> Max Time: %f secs <br/> </p> </HTML>",date_time,no_of_loops_output1,avg_loop_time_output1,data_to_file[0],data_to_file[1],min_output1,max_output1,loop_variable,avg_loop_time_output2,data_to_file[3],data_to_file[4],min_output2,max_output2);
if(pFile!=NULL)
{
fprintf(pFile,write_to_file);
close(pFile);
}
// Converting the HTML file to the PDF file
sprintf(command,"sudo -u user_name wkhtmltopdf savelog.html test.pdf");
gint gi;
gchar* standard_output;
gchar* standard_error;
GError* err = NULL;
// Making this process wait until the "command" exexution gets completed
g_spawn_command_line_sync
(
command,
&standard_output,
&standard_error,
&gi,
&err
);
if (err)
{
fprintf(stderr, "error: %s\n", err->message);
}
正在生成文件,但文件内容为空白。 当我从终端使用相同的HTML文件运行命令时,生成的PDF是准确的。
所以我试图找出导致空白创建文件的问题在哪里。是关于我对g_spawn_command_line_sync()的实现还是与wkhtmltopdf的使用有关。