0

I created a project that allows converting HTML to PDF. I'm using phantom-render-stream to convert the HTML to PDF. My problem now, I failed to pass an object value to HTML. Below is my code.

app.js

var username = "AAA";
var pets = [
   {id: 1, name: "cat"},
   {id: 2, name: "dog"},
   {id: 3, name: "sugar glider"}
];
var address = {
    line1: "Street A123",
    town: "Edinburgh",
    country: "Scotland"
};

render('http://localhost:8888/createForm.html?username='+username+'&pets='+pets+'&address='+address
.pipe(fs.createWriteStream('customer.pdf'));

In createForm.html, I used an AngularJS to get and view the value to PDF.

createForm.html

<script>
        var app = angular.module('myApp', []);

        app.config(function($locationProvider){
            $locationProvider.html5Mode(true);
        });

        app.controller('getDataCtrl', function($scope, $location) {
                         var url = decodeURIComponent( $location.url() );
             var value = $location.search();

            $scope.username = value.username;
            $scope.pets = value.pets;
                       $scope.address = value.address;
            })
</script>

<body>
      <div ng-app="onePayForm" ng-controller="getDataCtrl">
           <p>{{username}}</p>
           <p>{{pets[0].name}}</p>
           <p>{{address.line1}}</p>
      </div>
</body>

After successfully converting HTML to PDF, I opened the pdf file to see the result. Only username appears in pdf, the rest shows like this {{pets[0].name}} {{address.line1}}.

1
  • Post a working jsfiddle Commented Jul 10, 2017 at 9:52

1 Answer 1

2

in

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+pets+'&address='+address)
    .pipe(fs.createWriteStream('customer.pdf'));

your query params need to be prepared before appending them to the url. What you should do

var readyPets = encodeURIComponent(JSON.stringify(pets));
var readyAddress = encodeURIComponent(JSON.stringify(address));

then change the code above to :

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+readyPets+'&address='+readyAddress)
        .pipe(fs.createWriteStream('customer.pdf'));

in createForm.html parse these param queries:

$scope.pets = JSON.parse(decodeURIComponent(value.pets));
$scope.address = JSON.parse(decodeURIComponent(value.address));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Its worked! Now I know how to use encodeURIComponent, decodeURIComponent, JSON.stringify and JSON.parse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.