• Hi there!

    I have a strange problem with the handling of inline PDFs. The are being generated and presented using a rewrite endpoint ‘pdf’ after each of my posts.

    If caching is disabled, the PDFs are rendered fine. If WP Super Cache is active, the generating of the PDF still works (they are stored on my server), but the display in the browser is broken.

    Here are the response headers:

    
    Cache-Control: must-revalidate, post-check=0, pre-check=0
    Connection: Keep-Alive
    Content-Disposition: inline; filename="my-generated-pdf.pdf"
    Content-Transfer-Encoding: binary
    Content-Type: application/pdf
    Date: Fri, 12 Apr 2019 08:10:21 GMT
    Expires: 0
    Keep-Alive: timeout=5, max=99
    Pragma: public
    Server: Apache
    Set-Cookie: qtrans_front_language=de; expires=Sat, 11-Apr-2020 08:10:21 GMT; Max-Age=31536000; path=/
    Transfer-Encoding: chunked
    X-Powered-By: PHP/7.2.10
    
    • This topic was modified 7 years, 1 month ago by Rasso Hilber.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Rasso Hilber

    (@nonverbla)

    These two errors are what I get when trying to access the /pdf/ endpoint with WPSC enabled:

    
    PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 76291520 bytes) in /wp-includes/functions.php on line 3778
    
    PHP Fatal error:  Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0
    
    Thread Starter Rasso Hilber

    (@nonverbla)

    OK, seems like this is caused by Super Cache trying to store the whole PDF file. First I was able to make it work by increasing the memory limit in php.ini:

    
    memory_limit = 512M
    

    …but then I also found a solution to also make it work if the memory limit can’t be increased:

    • I activated the WP Super Cache-Option ‘Late init’
    • I put this in my theme:
    
    global $cache_enabled;
    if( is_pdf_url() ) {
      $cache_enabled = false;
    }
    function is_pdf_url() {
      $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
      return string_ends_with( $request_uri, '/pdf/' );
    }
    function string_ends_with($haystack, $needle) {
      $length = strlen($needle);
      if( $length == 0 ) {
        return true;
      }
      return ( substr($haystack, -$length) === $needle );
    }
    
    Saša

    (@stodorovic)

    It seems as an issue related to overlapping output buffers. It seems that there are stacked OB (WPSC + pdf). I can’t tell more without detail checking. Does “pdf endpoint” create ob? If so, could you sent snippet from function related to shutdown handler (or output buffer handler)?

    It’s possible to create simple WPSC plugin to you avoid “Late init”.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘/pdf/ corrupt PDF’ is closed to new replies.