Code Monkey home page Code Monkey logo

hugo-embed-pdf-shortcode's Introduction

License visitors

hugo-embed-pdf-shortcode


Table of Contents


Introduction

[Back to Top]

This is a Hugo Shortcode developed for use in Hugo based websites. This shortcode allows you to embed a PDF file in a page on your Hugo website. It is developed using the PDF.js library by Mozilla.

hugo-embed-pdf-shortcode cover

Setup

[Back to Top]

Note: This shortcode is for use in Hugo based websites. It will not work anywhere else.

Hugo embed-pdf can be installed in two ways.

Method 1 - Install as a Git submodule

  1. Add this shortcode as a Git submodule
git submodule add  https://github.com/anvithks/hugo-embed-pdf-shortcode.git themes/hugo-embed-pdf-shortcode
  1. Edit config.toml as follows
theme = ["hugo-embed-pdf-shortcode", "YourCurrentTheme"]
enableInlineShortcodes = true

To learn more about "Theme components", see the Hugo documentation


Method 2 - Clone this repository

  1. Clone this repository

git clone https://github.com/anvithks/hugo-embed-pdf-shortcode.git
cd hugo-embed-pdf-shortcode
  1. Copy the file ./layouts/shortcodes/embed-pdf.html to ./layouts/shortcodes in your Hugo website directory.

Note: If you do not have a ./layouts/shortcodes directory you can create it.

cp ./layouts/shortcodes/embed-pdf.html /path/to/your/hugo/website/layouts/shortcodes

  1. Copy the pdf.js library files from ./static/js/pdf-js to ./static/js in your Hugo website directory.

Note: If you do not have a ./static/js directory you can create it.

cp -R ./static/js/pdf-js /path/to/your/hugo/website/static/js/

Usage

[Back to Top]

In your Hugo website place the following shortcode in any of the markdown pages.

{{< embed-pdf url="./path/to/pdf/file/example.pdf" >}}

To hide pagination

{{< embed-pdf url="./path/to/pdf/file/example.pdf" hidePaginator="true" >}}

To render a selected page number

{{< embed-pdf url="./path/to/pdf/file/example.pdf" renderPageNum="5" >}}

To hide loading spinner

{{< embed-pdf url="./path/to/pdf/file/example.pdf" hideLoader="true" >}}

Parameters

  • url (required) : The relative location of the file.

  • hidePaginator (optional): Boolean which expects true or false. Hides the paginator for single page documents.

  • renderPageNum (optional): Integer which expects any number from 1 up to the last page number in the document. Will render that specific page on initial load.

  • hideLoader (optional): Boolean which expects true or false. Hides the loading spinner while your document loads.


Note: Currently supports local file embed. If absolute URL from the remote server is provided, configure the CORS header on that server.

FAQ

[Back to Top]

  1. I have installed hugo-embed-pdf in my website locally by cloning the repository and copying the files, but it does not work?
    A. hugo-embed-pdf uses pdf.js from mozilla. Pdf.js is now being served using a CDN.
    If you would like to use a local copy of PDf.js then you can make the following changes to the embed-pdf.html file.
  • Change the script tag at the top of the file from
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js" integrity="sha256-UZQVSEoMbJ82/3uFjt4mYOTVVHIImtkp7u3L6LMH6/Y=" crossorigin="anonymous"></script>

to

<script type="text/javascript" src='{{"/js/pdf-js/build/pdf.js" | relURL}}'></script>
  • Change the path to the pdf.worker.js file at line number 124 from
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js';

to

pdfjsLib.GlobalWorkerOptions.workerSrc = "{{.Site.BaseURL}}" + 'js/pdf-js/build/pdf.worker.js';

Support

[Back to Top] You an reach me at:

For any bugs, enhancement requests, feature requests please raise issues here

Who uses Hugo Embed Pdf Shortcode

[Back to Top]

Dirk's Changelog
SYSADMIN - Administration, security and hardening of Linux

License

[Back to Top]

License

hugo-embed-pdf-shortcode's People

Contributors

andream16 avatar anvithks avatar dicky1 avatar ericswpark avatar jeremybohannon avatar roneoorg avatar toinetoine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hugo-embed-pdf-shortcode's Issues

I'm sorry but, it doesn't work for me

First of all, thanks for your attention.
I am following the steps of his instructions but the pdf that is a single page does not load, only the window appears with "Previous Next Page: /" I have tried with firefox chrome and google chrome movile ..
I have also tried to load other pdf files with more than one page but I have the same result.
I don't know if I'm doing something wrong (I'm just starting with hugo), I'm trying to make it work at https://hotspotcepeda.github.io/es/posts/hotspot/#pdf
I have the pdf file uploaded locally at https://github.com/hotspotcepeda/hotspotcepeda.github.io/blob/main/static/pdfs/hotspot_conectar.pdf
I would like to know what is happening, I do not have an error message, the pdf file is simply not displayed.

I would appreciate your help in making it work.

Provide option to allow users to download the PDF file

What?
Provide a option to allow the user to download the PDF file.

Why?
Sometimes I need to publish PDF in “secure mode” (I mean that people don’t have to can downloading the PDF); other times, I need to publish PDF allowing people to download.
Currently, I seem that it isn’t possible to download the PDF; am I wrong?

Originally posted by @nicfab in #26 (comment)

Please make PDF resizable

Is your feature request related to a problem? Please describe.

No problem.

Describe the solution you'd like

It would be nice to either have the PDF resizable or have a parameter for the embed code to make the PDF larger (maybe in percent).

Undefined is not an object

I have this issue , with Safari 15.5 on Monterey 12.4
running Hugo server
undefined is not an object (evaluating 'pdfjsLib.GlobalWorkerOPtions...
Screenshot 2022-06-20 at 13 13 55

thanks in advance
best regards

PDF not loading on Netlify but is OK on localhost

Describe the bug
A clear and concise description of what the bug is.
When I upload my site to Netlify the spinner keeps on spinning but at the localhost it loads in a very short time.

To Reproduce
Steps to reproduce the behavior:
Open page with the "hugo-embed-pdf-shortcode"

Expected behavior
A clear and concise description of what you expected to happen.
I expect to see the PDF file

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Ubuntu 23.04
  • Browser [e.g. chrome, safari]
  • Brave browser, Firefox, Chrome
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.
I had installed it and the problem started where it was not loading on Netlify and had stopped loading on localhost. I then reinstalled it and it started reloading in localhost but not on Netlify.
I think I have read of other people having this problem with Netlify but I didn't see any solutions to the problem.

Scroll through pages

I use the Embed-pdf-shortcode for displaying my CV on my personal website.

I wonder if there could be an option in which the few pages will display automatically one after the other (as a long page) without the need to press next/previous.

unable to load pdf.worker.js with a baseURL in hugo

Describe the bug
in a hugo page with a different root page than ./ such as: localhost:1313/hugopage/, the js-pdf can not load, getting:

Setting up fake worker failed: "Cannot load script at: https://localhost:1313/js/pdf-js/build/pdf.worker.js".

To Reproduce
Steps to reproduce the behavior:

  1. run: hugo server --bind "" --baseURL "localhost/multimedia/"
  2. Try to load a markdown with {{< embed-pdf url="essai.pdf">}}

Expected behavior
The pdf should load correctly

Desktop (please complete the following information):

  • Linux
  • Firefox/Chrome

Additional context
Very important to change the BaseURL

Install as a Theme Component

Installation and updates would be easier with a Theme Component.
I just did a successful installation this way and I can provide a PR.
But before tackling this I would like to know if this PR has any chance to get merged.
What do you think anvitks, is it the right way to go?

Users can view PDFs but preventing them the download

Is your feature request related to a problem? Please describe.
Sometimes allowing users only to view some PDFs is preferable, preventing them from downloading them.
Sometimes you may want to share PDFs, but you want to prevent the download for various reasons.

Describe the solution you'd like
So, you can implement the plugin code with a feature that allows avoiding downloading PDFs.
In that way, PDFs can remain "embedded" in the webpage, and users can only view them page by page, but they should not be allowed to download PDFs. It's like a security mode.

I much appreciate it if you consider this suggestion soon.
See issue #26

[Feature Request] Add GitHub action to implement a CI for the shortcode

Is your feature request related to a problem? Please describe.
Currently there is no CI in place to test whether the changes made to the shortcode work in the real world.
Any change needs to be installed on a demo site manually and checked in the CI for the demo website
Demo site Source code is here

Describe the solution you'd like
When new code is added to the shortcode the CI should take the code from the PR, spawn the website code with the latest shortcode and check for any build errors and show a deploy preview. This is implemented in the Demo site using the netlify integration.

Describe alternatives you've considered
Currently no alternatives considered. Open to suggestions and any PRs that can implement a solution for this.

PDF.js library loading broken

Describe the bug
The script src is incorrect, which means the page breaks as the library will not load.

To Reproduce
Steps to reproduce the behavior:

  1. Include the library using the steps in README.
  2. Run the local webserver with hugo serve -D or deploy

Expected behavior
The PDF should display normally.

Screenshots
image

The correct URL should be /js/pdf-js/..., without the extra slash at the beginning. Manually removing the slash fixes the bug.

Reverting 704485b fixes the bug as well, but it will break if your site depends on the prepended relative link.

It doesn't work

I did according to the instructions.
However, it doesn't work.
Anything is visible on the page.

Use CDN instead of including the library files

Is your feature request related to a problem? Please describe.
No.

Describe the solution you'd like
At the moment the shortcode requires the user to include the library files locally. This will be a maintenance problem as the library will have to be updated everytime there is a new release.

Describe alternatives you've considered
Ideally the library can be included using a CDN. If not available then consider other projects / libraries that include PDF.js and provide a CDN.

Additional context

Explore the option of making the source of the PDF.js library configurable.
Refer this

PDF always loading, never displaying.

The initial installation did not work at all. But then I ran across this guide and I can get the shortcode to start loading the PDF, but it never actually loads it: the wheel just keeps on spinning.

how to set a pdf file so that it can display?

Screenshot_20210305_224426

I tried the inspect element on the Hugo blog page, an error appeared like the picture

{{< embed-pdf url="./path/to/pdf/file/LAPORAN HASIL MONITORING KEAMANAN SIBER TAHUN 2020.pdf" >}}

is it correct to set the url?

hidePaginator option does not work

Describe the bug
Option hidePaginator="true" has no effect.

To Reproduce
Steps to reproduce the behavior:
Insert shortcode to display with hidePaginator="true"

Expected behavior
I just want to display the first and only page of a pdf, so I don't need a paginator
or a link to download the PDF.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Ubuntu Linux
  • Browser Chrome
  • Version hugo v0.125.4

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Deploy pdf is not working

sorry i reopened the issue. this time I get an error that the pdf does not appear again when deployed on netlify

Screenshot_20210308_125753

Add a progress loader before displaying the pdf file

Is your feature request related to a problem? Please describe.
When embedding large PDF files the canvas is shown but the pdf is not rendered. The script waits for the PDF to load completely and then displays it.
This may take several minutes and during this period the page does not indicate any progress or loading information.

Describe the solution you'd like
Display a loading symbol or icon or progress percentage.

Describe alternatives you've considered

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.