I'm working on project where I would like to directly embed PDFs onto web pages. We'll be using Google's PDF viewer as the embed tool and implementing via a macro.
Here is the macro as it stands now:
{% macro pdfEmbed(pdfLink, width="580", height="480") %}
<iframe src="https://drive.google.com/viewerng/viewer?embedded=true&url={{ pdfLink }}" width="{{width}}px" height="{{height}}px"></iframe>
{% endmacro %}
For SEO purposes, I'd like to set a rel="canonical" on the PDF to point to the entry on which the PDF is embedded so Google knows to index the parent page and not the PDF. From what I've read, the general best practice for how to do this with embedded PDFs is via HTTP headers. See here for more detail: https://moz.com/blog/how-to-advanced-relcanonical-http-headers
I'm struggling with how to implement it in practice. I think the best method would be to use {% header %}
tag within the macro but I'm not sure if there's a way to target that at just PDF files? If that doesn't work, .htaccess could be an option but then I lose the ability to dynamically set the canonical on the PDF based on the URL of the entry that houses the PDF.
What is the best way to do this in Craft? I’m running a Craft 3 site FYI.
Thanks!