带有django的pdfkit由于网络错误而导致错误退出,代码为1:ContentOperationNotPermittedError

时间:2016-01-05 07:26:35

标签: python django wkhtmltopdf pdfkit

我使用pdfkit将html文件转换为pdf。当我在python终端中运行时它工作正常并转换相同的html文件。但是当我尝试从django shell运行相同的代码时,会出现此错误。

以下是给出的代码:

html_url = "/Users/swarna/workspace/server/"+todays_date+"/ret_"+packages['wbn']+".html"
pdf_url = "/Users/swarna/workspace/server/"+todays_date+"/retpdf_"+packages['wbn']+".pdf"
pdfkit.from_file(html_url,pdf_url)

它抛出了下面给出的错误:

*** IOError: wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://u (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://e (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://s (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://s (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://s (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://w (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://a (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://n (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://a (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://w (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://o (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://k (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://s (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://p (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://a (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://c (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://e (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://s (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://e (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://v (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://e (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://2 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://1 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://6 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://1 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:/// (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://5 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://r (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://e (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://t (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://_ (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://4 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://7 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://4 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://1 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://1 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://0 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://6 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://3 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page file:///Users/swarna/workspace/server/server_codes (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://6 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://h (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://t (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://m (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://l (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentOperationNotPermittedError

3 个答案:

答案 0 :(得分:1)

得到了解决方案。显然pdfkit无法找到wkhtmltopdf可执行文件。我们必须明确提到wkhtmltopdf的路径。

config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_file(html_url,pdf_url, configuration=config)

答案 1 :(得分:0)

在django中运行时,它认为您默认使用http://协议。这使得它在每个路径的开头添加http://。因此它假定参数是列表(因为pdfkit也接受列表)。这可能是因为this,但如果没有关于服务器设置的更多细节,我无法肯定地说。

您可以尝试以下方法:

html_url = "file:///Users/swarna/workspace/server/"+todays_date+"/ret_"+packages['wbn']+".html"
pdf_url = "file:///Users/swarna/workspace/server/"+todays_date+"/retpdf_"+packages['wbn']+".pdf"
pdfkit.from_file(html_url,pdf_url)

额外的file://强制django和pdfkit在你的文件系统中搜索。

答案 2 :(得分:0)

不确定原因,但将输入文件放入列表似乎可以解决问题:

mysql> DELIMITER //

mysql> DROP PROCEDURE IF EXISTS `new_procedure`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`(
    ->   IN `date_x` DATE,
    ->   OUT `emplist3` VARCHAR(200),
    ->   OUT `emplist4` VARCHAR(200)
    -> )
    -> BEGIN
    ->   SELECT 'EMPLOYEE.NAME 1' INTO `emplist3`
    ->   FROM DUAL
    ->   WHERE `date_x` = `date_x`;
    -> 
    ->   SELECT 'EMPLOYEE.NAME 2' INTO `emplist4`
    ->   FROM DUAL
    ->   WHERE `date_x` = `date_x`;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> SET @`NAME1` := NULL,
    ->     @`NAME2` := NULL;
Query OK, 0 rows affected (0.00 sec)

mysql> CALL `new_procedure`(DATE('2000-01-01'), @`NAME1`, @`NAME2`);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @`NAME1`, @`NAME2`;
+-----------------+-----------------+
| @`NAME1`        | @`NAME2`        |
+-----------------+-----------------+
| EMPLOYEE.NAME 1 | EMPLOYEE.NAME 2 |
+-----------------+-----------------+
1 row in set (0.00 sec)