====================================== 1.1. Console or GUI - Getting Started ====================================== A Complete example for creating a hello world PDF file in a console application or GUI front end. For us, we have just created a new dotnet core console application in Visual Studio. 1.1.1. Nuget Packages ---------------------- Make sure you install the Nuget Packages from the Nuget Package Manager ``_ This will add the latest version of the Scryber.Core nuget package. 1.1.2. Add a document template ------------------------------- In our applications we like to add our templates to a PDF folder. You can break it down however works for you, but for now a create a new XHML file called HelloWorld.html in your folder. And paste the following content into the file .. code-block:: html Hello World
Hello World from scryber.
1.1.3. File properties ----------------------- In the file properties for the HelloWorld.html file: Set the Build Action to None (if it is not already) And the Copy to output to Always. Your solution should look something like this. .. image:: ../images/initialhelloworldgui.png 1.1.4. Program code -------------------- In your program.cs add the namespace to the top of your class. .. code-block:: csharp using Scryber.Components; 1.1.5. Replace your main method. ----------------------------------- Next change the 'Main' method to your class to load the template and generate the pdf file .. code-block:: csharp static void Main(string[] args) { System.Console.WriteLine("Beginning PDF Creation"); //Get the working and temp directory string workingDirectory = System.Environment.CurrentDirectory; string tempDirectory = System.IO.Path.GetTempPath(); //The path to the input template - could be a stream, text reader, xml reader, resource etc var path = System.IO.Path.Combine(workingDirectory, "PDFs\\HelloWorld.html"); //The path to the output file - could be a stream var output = System.IO.Path.Combine(tempDirectory, "HelloWorld.pdf"); //Load the template and output to the directory var doc = Document.ParseDocument(path); doc.SaveAsPDF(output, System.IO.FileMode.OpenOrCreate); //Notify completion System.Console.WriteLine("PDF File generated at " + output); System.Console.ReadKey(); } .. image:: ../images/programcs.png The parser will read the document from the XHTML content, and then create a new PDF document in the tempDirectory for the output. 1.1.6. Testing your code ------------------------- Running your application, you should see the console output the path to the pdf. And opening this will show you the file. you could have saved it to a share, opened in Acrobat reader, or sent via email as a stream attachment. .. image:: ../images/helloworldconsole.png 1.1.7. Adding dynamic content ------------------------------ One of the driving forces behind scryber is the separation of the content, data and style. It is common practice in sites. With scryber all attributes and content is bindable to the data you want to pass to it, So we can specify our model data with from any source (here we are just using a dynamic object). And we can pass it to the parsed document either explicitly, or using the special 'model' overload on the PDF extension method. .. code-block:: csharp private static dynamic GetHelloWorldData() { //get your model data however you wish //it's just a sample object for this one. var model = new { titlecolor = "#ff6347", //style data title = "scryber", //simple content items = new[] //or even complex object data { new { name = "First item" }, new { name = "Second item" }, new { name = "Third item" }, } }; return model; } static void Main(string[] args) { System.Console.WriteLine("Beginning PDF Creation"); string workingDirectory = System.Environment.CurrentDirectory; string tempDirectory = System.IO.Path.GetTempPath(); var path = System.IO.Path.Combine(workingDirectory, "PDFs\\HelloWorld.html"); var output = System.IO.Path.Combine(tempDirectory, "HelloWorld.pdf"); var doc = Document.ParseDocument(path); //Assign the data model to a parameter doc.Params["model"] = GetHelloWorldData(); doc.SaveAsPDF(output, System.IO.FileMode.OpenOrCreate); //Notify completion System.Console.WriteLine("PDF File generated at " + output); System.Console.ReadKey(); } The general syntax for referring paramters in a template is {{**parameter[.property]**}} And the html5 tag 'template' is used with the data-bind attribute to loop over one or more items in a collection, and the inner objects and properties can be used with the '.' prefix to reference the current data context. So we can expand our document body to use the model schema. .. code-block:: html

{{concat("Hello from ",model.title)}}

We hope you like it.
.. image:: ../images/HelloWorldWithData.png 1.1.8. Adding Fonts and Styles ------------------------------- It's good but rather uninspiring. With scryber we can use css styles, just as we would in html. Here we are: * Adding a stylesheet link to the google 'Fraunces' font with the @font-face at-rule (watch that &display=swap link - it's not xhtml) * Adding some document styles for the body with fall-back fonts. * A complex style for a page header, with a colour and single background image, that will be repeated across any page. * And a page footer table with full width and associated style on the inner cells, that will again be repeated. The css style could just have easily come from another referenced stylesheet. Do not forget to encode the & character as & .. code-block:: html Hello World

Scryber document creation

{{concat("Hello from ",model.title)}}

We hope you like it.
Make some minor changes to our model. .. code-block:: csharp using Scryber.Components; var model = new { author = "Scryber Engine", titlecolor = "#ff6347 font-family:'Fraunces'", //style data ... The output from this is much more pleasing. Especially that Fruances font :-) .. image:: ../images/HelloWorldWithStyle.png 1.1.9. Further reading ---------------------- You can read more about the what you can do with scryber from the contents. We have no idea what you will be able to create with scryber. It's just there to hopefully help you build amazing documents in an easy and repeatable way.