• Hi, so we run both HTTP and HTTPS, as a result when a page uses the PDF viewer plugin and a user views the page via https but the pdf is linked using http, it causes a cross-origin request error.

    Is there a way to give PDF Viewer a relative link so a person visiting from http or https will see receive the proper pdf link in the viewer, thus avoiding the cross-origin request error?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can try to force the connection over HTTPS in your .htaccess. Please go to your FTP program, dowload the .htaccess file in your core WordPress root, open it with a notepad, change the code between the #begin and #end code with the following code:

    # BEGIN WordPress

    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    Save your file and upload it back to the root file of your website. Before you change something, please make a back-up of your .htaccess file!

    So I tried that. Used cyberduck to download the hidden .htaccess file that was at the root. Opened it with sublime text, saved the original, made the edit.

    Opened an incognito window in Chrome and couldn’t open the site because it ‘gave too many redirects’.

    Overwrote with the original .htaccess and it opened fine.

    Then I tried to add the two lines:

    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    to the original .htaccess file and that also didn’t work.

    So for now I’m looking at a pdf viewer that doesn’t view pdfs through https?

    Should I look for another solution? This has worked great up until this point.

    chidoti

    (@chidoti)

    I love this plugin as it shows everything crisp unlike google docs embed. I was looking how to fix it and found that the problem is in pdf-viewer.php.
    FILTER_VALIDATE_URL doesn’t work with protocol relative url. So I used following idea from someone on the internet:
    public function validate($input)
    {
    $valid = filter_var($input, FILTER_VALIDATE_URL);

    + // Simple workaround for protocol relative urls.
    + // If sticking a protocol on the front makes it valid, assume it’s valid
    + if(!$valid)
    + $valid = filter_var(‘http:’.$input, FILTER_VALIDATE_URL);

    return !!$valid;
    }

    Change the function add_shortcode() with following code (it’s works the same as the example above actually)

    public function add_shortcode( $atts, $content = "" ) {
    		
    
    		
    		if ( !empty($content) && filter_var($content, FILTER_VALIDATE_URL) ) {
    
    			//TODO: filter URL to check if PDF only
    			
    			
    			if ( $this->older_ie($this->options['olderIE']) ) {
    				
    				$notice = str_replace('%%PDF_URL%%', $content, $this->options['ta_notice']);
    				echo html_entity_decode($notice);
    				
    			} else {
    				
    				$atts = shortcode_atts(
    					array(
    						'width' => $this->options['tx_width'],
    						'height' => $this->options['tx_height'],
    						'beta' => empty($this->options['beta']) ? 0 : "true",
    					), 
    					$atts,
    					'pdfviewer' 
    				);
    				
    				$pdfjs_mode = ( $atts['beta'] === "true" ) ? 'beta' : 'stable';
    				$pdfjs_url = plugin_dir_url( __FILE__ ).$pdfjs_mode.'/web/viewer.html?file='.$content;
    				
    				$pdfjs_iframe = '<iframe class="pdfjs-viewer" width="'.$atts['width'].'" height="'.$atts['height'].'" src="'.$pdfjs_url.'"></iframe> ';
    				
    				return $pdfjs_iframe;
    			}
    		} else {
    			
    			if ( !empty($content) && filter_var('https:'.$content, FILTER_VALIDATE_URL) ) {
    
    			//TODO: filter URL to check if PDF only
    			
    			
    				if ( $this->older_ie($this->options['olderIE']) ) {
    					
    					$notice = str_replace('%%PDF_URL%%', $content, $this->options['ta_notice']);
    					echo html_entity_decode($notice);
    				
    				} else {
    				
    					$atts = shortcode_atts(
    						array(
    							'width' => $this->options['tx_width'],
    							'height' => $this->options['tx_height'],
    							'beta' => empty($this->options['beta']) ? 0 : "true",
    						), 
    						$atts,
    						'pdfviewer' 
    					);
    				
    					$pdfjs_mode = ( $atts['beta'] === "true" ) ? 'beta' : 'stable';
    					$pdfjs_url = plugin_dir_url( __FILE__ ).$pdfjs_mode.'/web/viewer.html?file='.$content;
    				
    					$pdfjs_iframe = '<iframe class="pdfjs-viewer" width="'.$atts['width'].'" height="'.$atts['height'].'" src="'.$pdfjs_url.'"></iframe> ';
    				
    					return $pdfjs_iframe;
    				}			
    			} else {
    
    				if ( !empty($content) && filter_var('http:'.$content, FILTER_VALIDATE_URL) ) {
    
    			//TODO: filter URL to check if PDF only
    			
    			
    				if ( $this->older_ie($this->options['olderIE']) ) {
    					
    					$notice = str_replace('%%PDF_URL%%', $content, $this->options['ta_notice']);
    					echo html_entity_decode($notice);
    				
    				} else {
    				
    					$atts = shortcode_atts(
    						array(
    							'width' => $this->options['tx_width'],
    							'height' => $this->options['tx_height'],
    							'beta' => empty($this->options['beta']) ? 0 : "true",
    						), 
    						$atts,
    						'pdfviewer' 
    					);
    				
    					$pdfjs_mode = ( $atts['beta'] === "true" ) ? 'beta' : 'stable';
    					$pdfjs_url = plugin_dir_url( __FILE__ ).$pdfjs_mode.'/web/viewer.html?file='.$content;
    				
    					$pdfjs_iframe = '<iframe class="pdfjs-viewer" width="'.$atts['width'].'" height="'.$atts['height'].'" src="'.$pdfjs_url.'"></iframe> ';
    				
    					return $pdfjs_iframe;
    				}			
    			} else {
    
    				return 'Invalid URL for PDF Viewer';
    			}
    
    			}
    		}
    	}
    • This reply was modified 7 years ago by chidoti.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Relative path / Unexpected server response’ is closed to new replies.