[Support Request]: Failed to use Export PDF #2514

Closed
opened 2026-02-05 04:22:27 +03:00 by OVERLORD · 19 comments
Owner

Originally created by @Buliwif on GitHub (Nov 29, 2021).

Attempted Debugging

  • I have read the debugging page

Searched GitHub Issues

  • I have searched GitHub for the issue.

Describe the Scenario

Hi,

I encounter a problem when I use export-books.php :
PHP Warning: file_get_contents(http://localhost/api/books?count=100&offset=0): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/userbookstack/export-books.php on line 72 PHP Fatal error: Uncaught TypeError: Return value of apiGetJson() must be of the type array, null returned in /home/userbookstack/export-books.php:81 Stack trace: #0 /home/userbookstack/export-books.php(48): apiGetJson('api/books?count...') #1 /home/userbookstack/export-books.php(20): getAllBooks() #2 {main} thrown in /home/userbookstack/export-books.php on line 81

First I have to explain my configuration, my bookstack is configured with SSL : https://bookstack.intra.local
This is the apache conf :
<VirtualHost *:443>
'ServerName bookstack.intra.local
ServerAdmin webmaster@localhost
DocumentRoot /var/www/bookstack/public/
SSLEngine on
SSLCertificateFile /etc/ssl/certs/bookstack.intra.local cer
SSLCertificateKeyFile /etc/ssl/private/bookstack.intra.local.key

My API Jeton is ok and this is the export-books.php file :
$apiUrl = getenv('BS_URL') ?: 'http://localhost'; // http://bookstack.local/
$clientId = getenv('BS_TOKEN_ID') ?: 'ThisIsMySecret';'
$clientSecret = getenv('BS_TOKEN_SECRET') ?: 'ThisIsMySecret';

Exact BookStack Version

21.08.5

Log Content

No response

PHP Version

No response

Hosting Environment

Ubuntu 18.4

Originally created by @Buliwif on GitHub (Nov 29, 2021). ### Attempted Debugging - [X] I have read the debugging page ### Searched GitHub Issues - [X] I have searched GitHub for the issue. ### Describe the Scenario Hi, I encounter a problem when I use export-books.php : `PHP Warning: file_get_contents(http://localhost/api/books?count=100&offset=0): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/userbookstack/export-books.php on line 72 PHP Fatal error: Uncaught TypeError: Return value of apiGetJson() must be of the type array, null returned in /home/userbookstack/export-books.php:81 Stack trace: #0 /home/userbookstack/export-books.php(48): apiGetJson('api/books?count...') #1 /home/userbookstack/export-books.php(20): getAllBooks() #2 {main} thrown in /home/userbookstack/export-books.php on line 81` First I have to explain my configuration, my bookstack is configured with SSL : **https://bookstack.intra.local** This is the apache conf : `<VirtualHost *:443>` ` 'ServerName bookstack.intra.local` ` ServerAdmin webmaster@localhost` ` DocumentRoot /var/www/bookstack/public/` ` SSLEngine on` ` SSLCertificateFile /etc/ssl/certs/bookstack.intra.local cer` ` SSLCertificateKeyFile /etc/ssl/private/bookstack.intra.local.key` My API Jeton is ok and this is the export-books.php file : `$apiUrl = getenv('BS_URL') ?: 'http://localhost'; // http://bookstack.local/` `$clientId = getenv('BS_TOKEN_ID') ?: 'ThisIsMySecret';'` `$clientSecret = getenv('BS_TOKEN_SECRET') ?: 'ThisIsMySecret';` ### Exact BookStack Version 21.08.5 ### Log Content _No response_ ### PHP Version _No response_ ### Hosting Environment Ubuntu 18.4
OVERLORD added the 🐕 Support label 2026-02-05 04:22:27 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Nov 29, 2021):

Hi @Buliwif,

From the first part of the error message:

PHP Warning: file_get_contents(http://localhost/api/books?count=100&offset=0):

The script you're running is attempting to fetch from http://localhost whereas you're saying you have BookStack at https://bookstack.intra.local.

You either need to configure the export-books.php file to set the $apiUrl as your instance base URL, Or you need to set a BS_URL environment variable when running the script to point at your instance.

@ssddanbrown commented on GitHub (Nov 29, 2021): Hi @Buliwif, From the first part of the error message: ``` PHP Warning: file_get_contents(http://localhost/api/books?count=100&offset=0): ``` The script you're running is attempting to fetch from `http://localhost` whereas you're saying you have BookStack at ` https://bookstack.intra.local`. You either need to configure the `export-books.php` file to set the `$apiUrl` as your instance base URL, Or you need to set a `BS_URL` environment variable when running the script to point at your instance.
Author
Owner

@Buliwif commented on GitHub (Nov 29, 2021):

As always, thank you for your quick response.

I forgot to say that I tried to set https://bookstack.intra.local in the export-books.php file. The result is :
PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /home/userbookstack/export-books.php on line 72 PHP Warning: file_get_contents(): Failed to enable crypto in /home/userbookstack/export-books.php on line 72 PHP Warning: file_get_contents(https://bookstack.intra.local/api/books?count=100&offset=0): failed to open stream: operation failed in /home/userbookstack/export-books.php on line 72 PHP Fatal error: Uncaught TypeError: Return value of apiGetJson() must be of the type array, null returned in /home/userbookstack/export-books.php:81 Stack trace: #0 /home/userbookstack/export-books.php(48): apiGetJson('api/books?count...') #1 /home/userbookstack/export-books.php(20): getAllBooks() #2 {main} thrown in /home/userbookstack/export-books.php on line 81

@Buliwif commented on GitHub (Nov 29, 2021): As always, thank you for your quick response. I forgot to say that I tried to set https://bookstack.intra.local in the export-books.php file. The result is : ` PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /home/userbookstack/export-books.php on line 72 PHP Warning: file_get_contents(): Failed to enable crypto in /home/userbookstack/export-books.php on line 72 PHP Warning: file_get_contents(https://bookstack.intra.local/api/books?count=100&offset=0): failed to open stream: operation failed in /home/userbookstack/export-books.php on line 72 PHP Fatal error: Uncaught TypeError: Return value of apiGetJson() must be of the type array, null returned in /home/userbookstack/export-books.php:81 Stack trace: #0 /home/userbookstack/export-books.php(48): apiGetJson('api/books?count...') #1 /home/userbookstack/export-books.php(20): getAllBooks() #2 {main} thrown in /home/userbookstack/export-books.php on line 81 `
Author
Owner

@ssddanbrown commented on GitHub (Nov 29, 2021):

@Buliwif Yeah, you'll get this with a self-signed certificate since it's not trusted by the instance you're running it on.
You'd either need to:

@ssddanbrown commented on GitHub (Nov 29, 2021): @Buliwif Yeah, you'll get this with a self-signed certificate since it's not trusted by the instance you're running it on. You'd either need to: - Add the HTTPS CA cert to the CA bundle used by PHP on the command-line machine. - or - Update the `file_get_contents` references to disable SSL verification, As per this stackoverflow post: https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-failed-to-enable-crypto
Author
Owner

@Buliwif commented on GitHub (Nov 29, 2021):

Thank You Dan,

I'll try it tomorrow and will tell you the result.

@Buliwif commented on GitHub (Nov 29, 2021): Thank You Dan, I'll try it tomorrow and will tell you the result.
Author
Owner

@Buliwif commented on GitHub (Dec 3, 2021):

Hi,
Sorry for the delay but I was focused on other kind of problem on my job :)

The HTTPS CA is already present in /etc/ssl/certs
And for the file_get_contents solution, I try to modify the export-books.php

Initial :
function apiGet(string $endpoint): string {
global $apiUrl, $clientId, $clientSecret;
$url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/');
$opts = ['http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"]];
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);

But I'm not sure of what I need to modify, (sorry but php is not my cup of tea... lol)

@Buliwif commented on GitHub (Dec 3, 2021): Hi, Sorry for the delay but I was focused on other kind of problem on my job :) The HTTPS CA is already present in /etc/ssl/certs And for the `file_get_contents` solution, I try to modify the export-books.php Initial : `function apiGet(string $endpoint): string {` ` global $apiUrl, $clientId, $clientSecret;` ` $url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/');` ` $opts = ['http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"]];` ` $context = stream_context_create($opts);` ` return file_get_contents($url, false, $context);` But I'm not sure of what I need to modify, (sorry but php is not my cup of tea... lol)
Author
Owner

@Buliwif commented on GitHub (Dec 6, 2021):

Hi,

So I finally tried this :

1st try
Modify the /usr/share/php/Composer/CaBundle/CaBundle.php
And add under $caBundlePaths = array( :
'/etc/ssl/certs/IntraLocalCA.cer'

systemctl restart php7.2-fpm.service apache2.service

Same problem

2nd try
Modify the /etc/php/7.2/apache2/php.ini
And add :
curl.cainfo = '/etc/ssl/certs/IntraLocalCA.cer'
openssl.cafile = '/etc/ssl/certs/IntraLocalCA.cer'
openssl.capath = '/etc/ssl/certs/IntraLocalCA.cer'

systemctl restart php7.2-fpm.service apache2.service

Same problem

@Buliwif commented on GitHub (Dec 6, 2021): Hi, So I finally tried this : **1st try** Modify the `/usr/share/php/Composer/CaBundle/CaBundle.php` And add under `$caBundlePaths = array(` : `'/etc/ssl/certs/IntraLocalCA.cer'` `systemctl restart php7.2-fpm.service apache2.service` > Same problem **2nd try** Modify the `/etc/php/7.2/apache2/php.ini` And add : `curl.cainfo` = '/etc/ssl/certs/IntraLocalCA.cer' `openssl.cafile` = '/etc/ssl/certs/IntraLocalCA.cer' `openssl.capath` = '/etc/ssl/certs/IntraLocalCA.cer' `systemctl restart php7.2-fpm.service apache2.service` > Same problem
Author
Owner

@ssddanbrown commented on GitHub (Dec 6, 2021):

@Buliwif The .ini to edit can depend on the operating system, PHP version in use and how you're actually running PHP.
If you're running from the command line, those changes probably won't make a difference.
Additionally, you're editing files for php7.2, whereas BookStack requires php7.3 or greater. (Assuming you're running the script on the same system as BookStack. If not, ignore this).

If you're running the scripts via command line, you can often find the right php.ini file by running:

php -i | grep php.ini

In regards to the option of editing the script:

For this function: f165f615fa/php-export-all-books/export-books.php (L64-L73)

Disabling SSL verification would look like this instead:

/**
 * Make a simple GET HTTP request to the API. (Ignoring HTTPS validation)
 */
function apiGet(string $endpoint): string {
    global $apiUrl, $clientId, $clientSecret;
    $url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/');
    $opts = [
        'http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"],
        'ssl'  => [        
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ];
    $context = stream_context_create($opts);
    return file_get_contents($url, false, $context);
}
@ssddanbrown commented on GitHub (Dec 6, 2021): @Buliwif The `.ini` to edit can depend on the operating system, PHP version in use and how you're actually running PHP. If you're running from the command line, those changes probably won't make a difference. Additionally, you're editing files for php7.2, whereas BookStack requires php7.3 or greater. (Assuming you're running the script on the same system as BookStack. If not, ignore this). If you're running the scripts via command line, you can often find the right `php.ini` file by running: ```bash php -i | grep php.ini ``` In regards to the option of editing the script: For this function: https://github.com/BookStackApp/api-scripts/blob/f165f615fae3b415cbf47a96661c4d43043e6a11/php-export-all-books/export-books.php#L64-L73 Disabling SSL verification would look like this instead: ```php /** * Make a simple GET HTTP request to the API. (Ignoring HTTPS validation) */ function apiGet(string $endpoint): string { global $apiUrl, $clientId, $clientSecret; $url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/'); $opts = [ 'http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ], ]; $context = stream_context_create($opts); return file_get_contents($url, false, $context); } ```
Author
Owner

@Buliwif commented on GitHub (Dec 6, 2021):

Hi Dan,

I finally found the php.ini few minutes before you wrote you message, and it works.

Thank you for taking time with me :)
Have a nice day.

@Buliwif commented on GitHub (Dec 6, 2021): Hi Dan, I finally found the php.ini few minutes before you wrote you message, and it works. Thank you for taking time with me :) Have a nice day.
Author
Owner

@ssddanbrown commented on GitHub (Dec 6, 2021):

Awesome! Will therefore close this off.

@ssddanbrown commented on GitHub (Dec 6, 2021): Awesome! Will therefore close this off.
Author
Owner

@Buliwif commented on GitHub (Dec 7, 2021):

Just an additional question please (or do you prefer I open a new issue ?)

Lot of pdf have a size of 0Kb ans are unreadable.
And I think it's linked, I have this error message when using export-books.php :

PHP warning: file_get_contents(https://bookstack.intra.local/api/books/19/export/pdf): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /export-books.php on line 90

Do you have an idea ?

@Buliwif commented on GitHub (Dec 7, 2021): Just an additional question please (or do you prefer I open a new issue ?) Lot of pdf have a size of 0Kb ans are unreadable. And I think it's linked, I have this error message when using export-books.php : `PHP warning: file_get_contents(https://bookstack.intra.local/api/books/19/export/pdf): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /export-books.php on line 90` Do you have an idea ?
Author
Owner

@ssddanbrown commented on GitHub (Dec 7, 2021):

You'll need to check the error log of your BookStack instance as per https://www.bookstackapp.com/docs/admin/debugging/.

My guess is potentially you're getting a timeout on exporting some books.

@ssddanbrown commented on GitHub (Dec 7, 2021): You'll need to check the error log of your BookStack instance as per https://www.bookstackapp.com/docs/admin/debugging/. My guess is potentially you're getting a timeout on exporting some books.
Author
Owner

@Buliwif commented on GitHub (Dec 7, 2021):

I think so it's a timeout cause it affect specially the biggest books, but can I easily increase this timeout ?

@Buliwif commented on GitHub (Dec 7, 2021): I think so it's a timeout cause it affect specially the biggest books, but can I easily increase this timeout ?
Author
Owner

@ssddanbrown commented on GitHub (Dec 7, 2021):

It's likely a task of increasing the max_execution_time value in your php.ini as per https://stackoverflow.com/questions/3829403/how-to-increase-the-execution-timeout-in-php.

There may also be time limits set on the webserver level.

@ssddanbrown commented on GitHub (Dec 7, 2021): It's likely a task of increasing the `max_execution_time` value in your `php.ini` as per https://stackoverflow.com/questions/3829403/how-to-increase-the-execution-timeout-in-php. There may also be time limits set on the webserver level.
Author
Owner

@Buliwif commented on GitHub (Dec 7, 2021):

Thank You, I'll try this way.

@Buliwif commented on GitHub (Dec 7, 2021): Thank You, I'll try this way.
Author
Owner

@Buliwif commented on GitHub (Dec 8, 2021):

Hi,

So, now I'm sure this is a timeout cause it happens only on the 2 biggest books.

So I modify php.ini :

max_execution_time=90
upload_max_files=20M

I check apache and the execution time is set by default at 300 seconds.

But it still don't work,
Is there something I miss ?

@Buliwif commented on GitHub (Dec 8, 2021): Hi, So, now I'm sure this is a timeout cause it happens only on the 2 biggest books. So I modify php.ini : `max_execution_time=90` `upload_max_files=20M` I check apache and the execution time is set by default at 300 seconds. But it still don't work, Is there something I miss ?
Author
Owner

@ssddanbrown commented on GitHub (Dec 8, 2021):

@Buliwif How long does it take before it times out?

You should be able to test this quickly via the standard BookStack user interface. Just export the book to PDF in the UI. Does the same actions in the background.

@ssddanbrown commented on GitHub (Dec 8, 2021): @Buliwif How long does it take before it times out? You should be able to test this quickly via the standard BookStack user interface. Just export the book to PDF in the UI. Does the same actions in the background.
Author
Owner

@Buliwif commented on GitHub (Dec 8, 2021):

It takes about 20 seconds, from interface and from cli. Far from the 90s I set in the php.ini.

@Buliwif commented on GitHub (Dec 8, 2021): It takes about 20 seconds, from interface and from cli. Far from the 90s I set in the php.ini.
Author
Owner

@ssddanbrown commented on GitHub (Dec 8, 2021):

@Buliwif Ah, this timeout is probably for a different php.ini than the one you edited for the certificate stuff. This'll be relevant to the php.ini used by Apache. Likely the /etc/php/<7.3 OR 7.4 OR 8.0>/apache2/php.ini file.

Remember to restart apache afterwards.

@ssddanbrown commented on GitHub (Dec 8, 2021): @Buliwif Ah, this timeout is probably for a different php.ini than the one you edited for the certificate stuff. This'll be relevant to the php.ini used by Apache. Likely the `/etc/php/<7.3 OR 7.4 OR 8.0>/apache2/php.ini` file. Remember to restart apache afterwards.
Author
Owner

@Buliwif commented on GitHub (Dec 9, 2021):

So strange...

I try to increase the both values on all my php.ini files :
max_execution_time=300
upload_max_files=20M
Moreover, I try to set no limit in all my php.ini files :
max_execution_time=0
Another test, I add this in the export-books.php :
set_time_limit (3000);

Same result...

@Buliwif commented on GitHub (Dec 9, 2021): So strange... I try to increase the both values on all my php.ini files : `max_execution_time=300` `upload_max_files=20M` Moreover, I try to set no limit in all my php.ini files : `max_execution_time=0` Another test, I add this in the export-books.php : set_time_limit (3000); Same result...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#2514