Best way to access external file

Hi there,
I would like to dysplay a pdf file in an iframe (for example) from his absolute path. what is the best way to do that ?

I attempt some things :

  • putting my pdf file in the public folder and accessing it with this :
    <iframe src="test.pdf"></iframe>
    It’s obviously working ! The problem is : my file will never be there in my project :slight_smile:

  • so i transformed my code with :
    <iframe src="/home/pierre/Documents/tmp/print/pdf/test.pdf"></iframe>
    Here I have no error, but a 404 not found. Normal thing, client can’t access to this kind of absolute path.

  • so i passed by the server, and trying to get the Binary with peerlibrary:pdf.js package :
    <iframe src="{{getPdf}}"</iframe>

I do a registerHelper with my getPdf who are executing a Meteor.call, and server side i get :

    "getPdf": function () {
      var pdf = {
                    data: Assets.getBinary("/home/pierre/Documents/tmp/print/pdf/test.pdf"),   // same result with only "test.pdf"
                    password: ''
                };
                var document = PDFJS.getDocumentSync(pdf);
                var page = document.getPageSync(1);
                return document;
    }

And here i get : Error: Unknown asset: /home/pierre/Documents/tmp/print/pdf/test.pdf

I’m stunned, any tips ?

Assets.getBinary can only be used to retrieve assets stored under an applications private directory. If you want to use peerlibrary:pdf.js, then you’ll need to use another method to load the PDF. For example, peer library:pdf.js itself recommends the following in its docs:

If not using Assets to get PDF, you should use fs package for file system access to get fibers-enabled synchronous functions instead of functions which block the whole node.js process.

That being said, maybe consider storing the PDF in your apps private directory to make things easier.

Hi Hwillson and thanks for your answer !
I passed like you said with the fs package. I did the meteor add peelibrary:fs and i did that code in the server :

        var dir = '/home/pierre/Documents/tmp/print/pdf/';
        var pdfFile = dir + "test.pdf";

        var pdf = {
            data: fs.readFileSync(pdfFile),
            password: ''
        };

        var document = PDFJS.getDocumentSync(pdfFile);
        return document;

But i still get an error :
Exception while invoking method ‘getPdf’ Error: MissingPDFException: Missing PDF “file:///home/pierre/Documents/tmp/print/pdf/test.pdf”.
But copying this path to my brower lead me to my pdf.

I conturned my problem by using iron:router to do the job
I read my pdf with a redirection like that :

Router.route('/pdf/:filename', function() {
    var filePath = "/home/pierre/Documents/tmp/print/pdf/" + this.params.filename;
    var fs = Npm.require('fs');
    var data = fs.readFileSync(filePath);
    this.response.write(data);
    this.response.end();
}, {
    where: 'server'
});

So when i do a link to localhost:3000/pdf/test.pdf, it open the file in the new tab.
That isn’t the expected solution… but the work is “done” :slight_smile:

PS : nope, i couldn’t put all my pdf in private or public folders cause they are dynamically created from java. And I don’t want put my files in there :slight_smile: