使用PDFObject下载带动态文件名的pdf

时间:2017-10-17 06:00:14

标签: javascript jquery html5 pdfobject

我正在尝试将PDFObject用于我的宣传册,我希望用户选择一辆车并下载她/他选择的特定车辆。这是我的代码:

<form method="get" action="">

  <div class="form-group"><h5>Model</h5></div>

  <div id="select-vehicle" class="form-group">
    <select class="form-control">
      <option selected disabled> Select Vehicles</option>
    </select>
  </div>

  <div class="form-group">
    <button type="submit" value="submit" id="submit" disabled class="btn btn-default">DOWNLOAD</button>
  </div>

</form>

这是我的工作javascript:

var workers = ["86", "Altis", "Avanza", "Altis", "Camry", "Fortuner", "FJ Cruiser"
            , "Hiace", "Hilux", "Innova", "LandCruiser", "Prado", "Previa", "PriusC"
            , "Rav4", "Vios", "Yaris"];
        for(var i=0; i< workers.length;i++)
        {
        //creates option tag
          jQuery('<option/>', {
                value: workers[i],
                html: '2017' + ' ' + workers[i] + ' ' + '&#174;'
                }).appendTo('#select-vehicle select'); //appends to select if parent div has id dropdown
        }
          $('button').click(function(event) {
            var value = $( "select" ).val();
            $('form').attr('action', '../../vehicles/' + value +'/');
          });

        $('select').change(function(){
          if ($(this).val())
          {
            $("button").removeAttr('disabled');
          }
        });

我想将此$('form').attr('action', '../../vehicles/' + value +'/');传递给PDFObject,以便找到正确的文件名来下载文件。

1 个答案:

答案 0 :(得分:1)

下载PDF

PDFObject用于在您的网页中嵌入pdf。要下载pdf,您只需转到该pdf,但您需要pdf文件名。你没有显示pdf文件的名称,所以这里有一个例子,如果文件名与select的值相同......

$('button').click(function(e) {
    e.preventDefault();
    var value = $('select').val();
    window.open('../../vehicles/' + value + '/' + value + '.pdf','_blank');
});

嵌入PDF

如果您想使用PDFObject在页面中嵌入该pdf(此选项不需要form,只需select),请先在您的容器中创建一个容器html举行pdf ...

<div id="vehiclepdf"></div>

...并在您点击按钮时调用PDFObject ...

$('button').click(function(e) {
    e.preventDefault();
    var value = $('select').val();
    PDFObject.embed('../../vehicles/' + value + '/' + value + '.pdf',"#vehiclepdf");
});