PDF is blank page when using CGPDFContextCreateWithURL

Tom

I'm trying to create a simple pdf with CGPDFContextCreateWithURL, but when I show the pdf it's blank. My code:

 override func viewDidLoad() {
    super.viewDidLoad()

drawSuperPDF("test4.pdf")

   showPDFFile()

}

My drawSuperPDF() function

func drawSuperPDF(filename: String) {

    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pdfFileName = documentsURL.URLByAppendingPathComponent(filename)
    let fullPath = pdfFileName.path!

    let url = NSURL(fileURLWithPath: fullPath)


    var mediaBox:CGRect = CGRectMake(0, 0, 612, 792)


    let myPDFContext = CGPDFContextCreateWithURL(url, &mediaBox, nil)


    drawHeaders()



CGPDFContextEndPage(myPDFContext)


}

My drawHeaders() and drawHeaderText() functions

func drawHeaders() {


    drawHeaderText(CGPoint(x: 30, y:  10), headerText: "Hello World:", underline: true)


    drawHeaderText(CGPoint(x: 30, y: 25), headerText: "Hello World:", underline: true)


    drawHeaderText(CGPoint(x: 30, y: 40), headerText: "Hello World:", underline: true)


    drawHeaderText(CGPoint(x: 30, y: 70), headerText: "Hello World:", underline: true)




}

func drawHeaderText(point: CGPoint, headerText: String, underline: Bool) {

    let drawingPoint = CGPoint(x: point.x, y: point.y)

    if underline {


        var multipleAttributes = [String : NSObject]()
        multipleAttributes[NSFontAttributeName] = UIFont.boldSystemFontOfSize(12)
        multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.StyleSingle.rawValue


        headerText.drawAtPoint(drawingPoint,
            withAttributes: multipleAttributes)


    } else {

        let font = UIFont.systemFontOfSize(12)
        headerText.drawAtPoint(drawingPoint,
            withAttributes: [NSFontAttributeName : font])}

}

And finally, my showPDFFile()

func showPDFFile() {

    let fileName = "test4.pdf"
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pdfFileName = documentsURL.URLByAppendingPathComponent(fileName)
    let fullPath = pdfFileName.path!
    let webView : UIWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
    let url : NSURL = NSURL(fileURLWithPath: fullPath)
    let request : NSURLRequest = NSURLRequest(URL: url)

    webView.scalesPageToFit = true
    webView.loadRequest(request)

    self.view.addSubview(webView)


}
David van Driessche

Before you start drawing on a PDF page, you need to "start" the page by calling CGPDFContextBeginPage. Calls to begin page and end page need to be balanced out by the way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect page displays blank when using $_POST

From Dev

Opening a PDF from $http.post crash when using _blank

From Dev

Why does my page page return blank when using a server name with mod_rewrite?

From Dev

How to get page number on dompdf PDF when using "view"

From Dev

setting pdf page width/height when using jsPDF

From Dev

Problem with empty page when using Apache PDFBox to add image to PDF

From Dev

setting pdf page width/height when using jsPDF

From Dev

When using Magento, PDF cuts off after first page

From Dev

Blank pages when converting to PDF with TuesPechkin

From Dev

Generating blank pdf when downloaded -ItextSharp

From Dev

Laravel 4 returns blank page when using return withInput (file attached)

From Dev

Vue 3 with Vite rendering blank page when not using App.vue

From Dev

Getting blank page when uploading an image in Laravel

From Dev

Blank page when trying to access phpmyadmin

From Dev

XML shows blank page when linking XSL

From Dev

Blank page when trying to access phpmyadmin

From Dev

How to prevent blank posting when page is refreshed

From Dev

XML shows blank page when linking XSL

From Dev

Blank page when call form helper CodeIgniter

From Dev

Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

From Dev

Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

From Dev

Getting blank page using RequireJS and Grunt

From Dev

First page of PDF's uploaded to web server appearing blank

From Dev

How do I insert a blank page into a PDF with ghostscript or pdftk?

From Dev

First page of PDF's uploaded to web server appearing blank

From Dev

Splitting a PDF file with Ghostscript results in one extra blank page

From Dev

Edit pdf page using pdfbox

From Dev

Replace PDF page using PDFBox

From Dev

Blank page when creating dynamic page with PHP MySQL

Related Related

  1. 1

    Redirect page displays blank when using $_POST

  2. 2

    Opening a PDF from $http.post crash when using _blank

  3. 3

    Why does my page page return blank when using a server name with mod_rewrite?

  4. 4

    How to get page number on dompdf PDF when using "view"

  5. 5

    setting pdf page width/height when using jsPDF

  6. 6

    Problem with empty page when using Apache PDFBox to add image to PDF

  7. 7

    setting pdf page width/height when using jsPDF

  8. 8

    When using Magento, PDF cuts off after first page

  9. 9

    Blank pages when converting to PDF with TuesPechkin

  10. 10

    Generating blank pdf when downloaded -ItextSharp

  11. 11

    Laravel 4 returns blank page when using return withInput (file attached)

  12. 12

    Vue 3 with Vite rendering blank page when not using App.vue

  13. 13

    Getting blank page when uploading an image in Laravel

  14. 14

    Blank page when trying to access phpmyadmin

  15. 15

    XML shows blank page when linking XSL

  16. 16

    Blank page when trying to access phpmyadmin

  17. 17

    How to prevent blank posting when page is refreshed

  18. 18

    XML shows blank page when linking XSL

  19. 19

    Blank page when call form helper CodeIgniter

  20. 20

    Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

  21. 21

    Using Rotativa to generate PDF From a view when the page breaks data continue on the header of the second page disregarding CSS

  22. 22

    Getting blank page using RequireJS and Grunt

  23. 23

    First page of PDF's uploaded to web server appearing blank

  24. 24

    How do I insert a blank page into a PDF with ghostscript or pdftk?

  25. 25

    First page of PDF's uploaded to web server appearing blank

  26. 26

    Splitting a PDF file with Ghostscript results in one extra blank page

  27. 27

    Edit pdf page using pdfbox

  28. 28

    Replace PDF page using PDFBox

  29. 29

    Blank page when creating dynamic page with PHP MySQL

HotTag

Archive